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
I was surprised (discovered in python/typeshed#9723), that typecheckers currently do not consider type in annotation context purely generic alias: unlike list or dict, it is allowed to remain unsubscripted. This results in unexpected behaviour like the issue above.
mypy issue (open): python/mypy#12301 pyright issue (closed "working as intended", with comment by Eric Traut I don't really understand - probably it's about inheritance from type): microsoft/pyright#4658
I think that this behaviour should be changed. I do understand possible effect on existing codebases (backwards incompatibility), but this is a thing really easy to fix: all occurrences of bare type in annotations have to be replaced with type[object] (or type[Any] to match curent behaviour, but it's probably worse and should be rarely needed). See typeshed stats at the end.
In addition to issue above, here are some other things to consider:
typing.Type without type argument is reported as error. This means that type and typing.Type are not equivalent in annotations context, and thus PEP585 deprecation can be harmful. From now, I'll switch back to typing.Type to be sure that mypy will point out missing part.
This thing is somewhat hidden: I wasn't aware of such type special treatment, and it is not obvious.
This is not absolutely straightforward: while missing generic args should be marked in annotations, it should not happen in class base (so that class MyMeta(type) typechecks). I don't see any other places where parameter omission should be allowed.
PEP585 does not mention any special type treatment.
All use cases in annotations can be covered with type[object] or type[Any].
Typeshed stats
For a quick estimate, I just grepped the typeshed for type usage in annotations. Here's the result (with unrelated lines mostly removed):
I was surprised (discovered in python/typeshed#9723), that typecheckers currently do not consider
type
in annotation context purely generic alias: unlikelist
ordict
, it is allowed to remain unsubscripted. This results in unexpected behaviour like the issue above.mypy
issue (open): python/mypy#12301pyright
issue (closed "working as intended", with comment by Eric Traut I don't really understand - probably it's about inheritance fromtype
): microsoft/pyright#4658I think that this behaviour should be changed. I do understand possible effect on existing codebases (backwards incompatibility), but this is a thing really easy to fix: all occurrences of bare
type
in annotations have to be replaced withtype[object]
(ortype[Any]
to match curent behaviour, but it's probably worse and should be rarely needed). See typeshed stats at the end.In addition to issue above, here are some other things to consider:
typing.Type
without type argument is reported as error. This means thattype
andtyping.Type
are not equivalent in annotations context, and thus PEP585 deprecation can be harmful. From now, I'll switch back totyping.Type
to be sure thatmypy
will point out missing part.type
special treatment, and it is not obvious.class MyMeta(type)
typechecks). I don't see any other places where parameter omission should be allowed.type
treatment.type[object]
ortype[Any]
.Typeshed stats
For a quick estimate, I just grepped the typeshed for
type
usage in annotations. Here's the result (with unrelated lines mostly removed):The text was updated successfully, but these errors were encountered: