You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error: Incompatible default for argument "default" (default has type "ellipsis", argument has type "_T") [assignment]
Somewhat misleadingly, bar: _T = None results in an error stating
PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
But this isn't implicitly optional: it's explicitly the same as Any.
Your Environment
Mypy version used: 1.7.1
Python version used: 3.10
Discussion on complexity
I realise this is likely to be a complex corner case to handle. In discussion it would be better to separate the complexity of changing this behaviour from the correctness of this behaviour.
There is a knock on inference here where there is more than one parameter using the same TypeVar. Eg:
defbaz(a: _T, b: _T=123) ->None:
....
Logically this would infer that these are subsequently all fine:
baz(321)
baz(321, 567)
baz("aaa", "bbb")
But this would be an error:
baz("aaa")
I realise that might be incredibly complex to implement.
At least...
It would be helpful if mypy could be more informative about this. If (mypy or python typing hinting in general) cannot reason with defaults and unbound TypeVars then it would be better for users to be told this.
The text was updated successfully, but these errors were encountered:
I am seeing a similar issue but with a bound TypeVar.
Perhaps subject could be edited and expanded by removing "unbounded" word?
See below:
fromdataclassesimportdataclassfromtypingimportTypeVar@dataclassclassLocation:
id: intname: strLocationT=TypeVar("LocationT", bound=Location)
# Location is the default model to use if not overriden; mypy complains about the assignmentdeftest_mypy(id_: int, name: str, model: type[LocationT] =Location) ->LocationT: # type: ignore[assignment]returnmodel(id_, name)
test_mypy(12, "Fred", Location) # mypy accepts Location here
Bug Report
Default values are incorrectly flagged as incompatible with unbound TypeVar.
To Reproduce
Expected Behavior
This should be fine. Unbound
typing.TypeVar("_T")
should accept the same astyping.Any
.For example this does work:
Actual Behavior
Somewhat misleadingly,
bar: _T = None
results in an error statingBut this isn't implicitly optional: it's explicitly the same as
Any
.Your Environment
Discussion on complexity
I realise this is likely to be a complex corner case to handle. In discussion it would be better to separate the complexity of changing this behaviour from the correctness of this behaviour.
There is a knock on inference here where there is more than one parameter using the same TypeVar. Eg:
Logically this would infer that these are subsequently all fine:
But this would be an error:
I realise that might be incredibly complex to implement.
At least...
It would be helpful if mypy could be more informative about this. If (mypy or python typing hinting in general) cannot reason with defaults and unbound TypeVars then it would be better for users to be told this.
The text was updated successfully, but these errors were encountered: