Understanding how a type checker processes frozenset([42]) #1328
Replies: 2 comments 14 replies
-
|
The
Strictly speaking, it's actually an implicit staticmethod rather than an implicit classmethod :) The Addressing your question, I can't comment on how type checkers solve the problem, exactly. But if the type checker should understand the type of the created reveal_type(frozenset([42])) # Revealed type is "builtins.frozenset[builtins.int]"The constructor is typed such that a type checker should be able to easily infer the type of calls to subclasses, even if the from typing import TypeVar
_T_co = TypeVar("_T_co")
class Foo(frozenset[_T_co]): ...
reveal_type(Foo([42])) # Revealed type is "__main__.Foo[builtins.int]" |
Beta Was this translation helpful? Give feedback.
-
|
There is still one more bit I don't quite understand. If |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I am trying to build a mental model for how a type checker resolves the type of
frozenset([42])and I'm stuck at type argument inference. Could someone help me understand what is going on here?frozensetis a generic class with a single covariant type parameter. It has an overloaded__new__classmethod which uses aSelftype:A type checker will select the second overload based on the number of arguments passed to the call. Note that the overload is not generic, because
__new__is (implicitly) a classmethod, so_T_coin the type of__iterableis bound by the class and not__new__. The type ofclsis less clear. According to PEP-673,However, since
__new__is called implicitly, that type is also implicit. Is itfrozenset[Any]orfrozenset[_T_co]? If the latter, how does this work exactly?Selfbound to that instantiation? i.e. shouldSelfin the return type also be treated as if it wasfrozenset[_T_co]?Beta Was this translation helpful? Give feedback.
All reactions