Use TypeVar defaults instead of Any when fixing TypeAlias types (PEP 696)#16825
Conversation
This comment has been minimized.
This comment has been minimized.
| TA1 = Dict[T2, T3] | ||
|
|
||
| def func_a1( | ||
| a: TA1, # E: Missing type parameters for generic type "TA1" |
There was a problem hiding this comment.
This errors seems like a false positive, since both of the type parameters have a default.
There was a problem hiding this comment.
Good catch! I added the --disallow-any-generics flag specifically to detect it, but somehow missed to check it. Pushed a new commit.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Looks good, just some nits
| b: TA4[float], # E: Bad number of arguments for type alias, expected between 2 and 3, given: 1 | ||
| c: TA4[float, float], | ||
| d: TA4[float, float, float], | ||
| e: TA4[float, float, float, float], # E: Bad number of arguments for type alias, expected between 2 and 3, given: 4 |
There was a problem hiding this comment.
nit: I don't think there should be a colon after "given". However, this looks to be consistent with a bunch of other errors, so we can leave it for now.
A similar runtime error for comparison:
python -c '''
def f(a: int, b: str = ""): ...
f(1, "x", 3)
'''
Traceback (most recent call last):
File "<string>", line 3, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
There was a problem hiding this comment.
I agree, however for this I tried to modify the existing message as little as possible, thus keeping the colon. If you like, I would suggest that I open a separate PR later to update all cases for "Bad number of arguments for type alias". That would be better since it requires changes to other test files as well.
| reveal_type(e) # N: Revealed type is "Tuple[Any, Any, builtins.int]" | ||
| [builtins fixtures/dict.pyi] | ||
|
|
||
| [case testTypeVarDefaultsTypeAlias2] |
There was a problem hiding this comment.
| [case testTypeVarDefaultsTypeAlias2] | |
| [case testParamSpecDefaultsTypeAlias] |
More informative test case name
There was a problem hiding this comment.
To be honest, I try to keep the same prefix for all cases to be able to do pytest -k testTypeVarDefaults. Maybe I'm a bit lazy in that regard 😅 Running the whole test file would also work but it's more to type.
pytest mypy/test/testcheck.py::TypeCheckSuite::check-typevar-defaults.test
--
IMO it also makes reading the test cases a bit easier since you can quickly see that all deal with ...TypeAlias. Similarly to the last PR were all used ...Class.
--
In the end it's probably just preference, so if you like me to I'll change it.
| reveal_type(d) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]" | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
||
| [case testTypeVarDefaultsTypeAlias3] |
There was a problem hiding this comment.
| [case testTypeVarDefaultsTypeAlias3] | |
| [case testTypeVarTupleDefaultsTypeAlias] |
Small update to adjust the error messages following the suggestion in #16825 (comment).
This PR applies the TypeVar defaults to
TypeAliastypes instead of usingAnyexclusively, similar to #16812.Again
TypeVarTupledefaults aren't handled correctly yet.Ref: #14851