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.
-
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?frozenset
is a generic class with a single covariant type parameter. It has an overloaded__new__
classmethod which uses aSelf
type: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_co
in the type of__iterable
is bound by the class and not__new__
. The type ofcls
is 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?Self
bound to that instantiation? i.e. shouldSelf
in the return type also be treated as if it wasfrozenset[_T_co]
?Beta Was this translation helpful? Give feedback.
All reactions