-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Explicit type declaration overridden/merged with Any #19034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Adding However, the error on the last line is really bad - we should not reject In other words, this is a bug IMO: from typing import Any
def report_errors(error_type: int, id_: str, messages: list[str]) -> None:
pass
any_: Any
messages: list[str]
report_errors(*any_) # OK
report_errors(*any_, messages) # E: Too many arguments for "report_errors" [call-arg] When there's |
Thanks for looking at this, I'd be interested to hear more about what's expected here. Note that the addition of the Even if adding Consider for example: foo: int | None
def bar(): ...
foo = bar()
reveal_type(foo) # previously: "Union[builtins.int, None]"
# now: "Union[builtins.int, Any]"
reveal_type(bar()) # "Any"
foo.bit_length() # previously: error: Item "None" of "int | None" has no attribute "bit_length" [union-attr]
# now: no error Which I think is equivalent -- the key thing is that previously the type explicitly declared was respected and this was a way to indicate what the type is locally when we know the return type even if it's not annotated. With the change, This does seem to be specific to the case of the union having |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
It appears that explicit type definitions of loop variables can be overridden or merged with values inferred from the iterator. Unfortunately this happens even when the iterator type is
Any
, somewhat undermining the explicit definition.To Reproduce
I've tried reproducing this without the loop, however that seems to make the issue go away.
Removing the
messages
argument toreport_errors
also seems to avoid the "too many arguments" error, though that doesn't affect the revealed types so I've left it in to give a clearer pass/fail when runningmypy
.Expected Behavior
Under 1.15 and up to 8dd616b the above code passes cleanly with the "good" noted revealed types.
Actual Behavior
From b50f3a1 onwards the above code fails type checking due to the errant message to the
report_errors
function, and outputs the "bad" noted revealed types. I'm not familiar enough with mypy to explain why that commit is the cause, however I have narrowed this to that commit using git bisect so I'm fairly confident those changes are related.Your Environment
Versions as noted above. No command line flags or other config needed.
Python 3.12.3 on Ubuntu 24.04.
The text was updated successfully, but these errors were encountered: