Distinct formatting for type objects#3374
Conversation
ilevkivskyi
left a comment
There was a problem hiding this comment.
Thanks! This mostly looks good to me, just one suggestion and one comment unrelated to this PR.
test-data/unit/check-generics.test
Outdated
| def __init__(self) -> None: pass | ||
| x = C # type: Callable[[], C[int]] | ||
| y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type C[T], variable has type Callable[[], int]) | ||
| y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type Type[C[T]], variable has type Callable[[], int]) |
There was a problem hiding this comment.
Probably unrelated to this PR, but this error message looks not right, it should be Type[C] here (or maybe Type[C[Any]]). If not and it is not easy to fix this (by just erasing the return type) in the same PR, could you please open a new issue?
There was a problem hiding this comment.
I am also not entirely sure I understand what's wrong with Type[C[T]]. Making this work seems to require either bringing erasure to this module or duplicating the formatting of instance types without type parameters.
There was a problem hiding this comment.
I am also not entirely sure I understand what's wrong with
Type[C[T]]
PEP 484 specifies that bare (unsubscribed) type is equivalent to C[Any]. Moreover, writing C[T] is invalid in this context (and in other examples I mentioned below) and will trigger a mypy error Invalid type '__main__.T'.
Does fixing this actually requires extensive code duplication? If yes, then maybe it is better to import erasetype.
test-data/unit/pythoneval.test
Outdated
| MyDDict(dict)[0] | ||
| [out] | ||
| _program.py:6: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str] | ||
| _program.py:6: error: Argument 1 to "defaultdict" has incompatible type Type[List[_T]]; expected Callable[[], str] |
There was a problem hiding this comment.
Again, this should be Type[List] or Type[List[Any]].
test-data/unit/check-functions.test
Outdated
| # M = Callable[[Arg(gnome='x', type=int)], int] E: Invalid type alias E: Unexpected keyword argument "gnome" for "Arg" | ||
| N = Callable[[Arg(name=None, type=int)], int] # ok | ||
| O = Callable[[List[Arg(int)]], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Type expected within [...] # E: The type List[T] is not generic and not indexable | ||
| O = Callable[[List[Arg(int)]], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Type expected within [...] # E: The type Type[List[T]] is not generic and not indexable |
There was a problem hiding this comment.
E: The type
Type[List[T]]is not generic and not indexable.
@sixolet This error looks quite confusing in this context, since the type mentioned is generic and is indexable. In real mypy installation (not in tests) I get these errors:
tst26.py:14: error: Invalid type alias
tst26.py:14: error: Value of type "object" is not indexable
tst26.py:14: error: Type expected within [...]
tst26.py:14: error: Value of type List[_T] is not indexable
Note that none of them mentions (or even hints) that something is wrong with Arg. Is there an issue for improving the error message in such cases?
ilevkivskyi
left a comment
There was a problem hiding this comment.
LGTM. If no one objects, then I will merge this soon.
|
Thank you! (Both.) |
Fix #3050. (The discussion there is not over yet, but this PR can demonstrate the formatting)