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
{{ message }}
Repository navigation
Type of first argument of class methods should not be 'type' #123
An exception is that the first argument of instance and
class methods does not need to be annotated; it is assumed to have the
type of the containing class for instance methods, and type for
class methods.
If the type of first argument of a class method is type, it wouldn't be possible to access other class attributes or methods via the argument. Consider this code:
class A:
n = 0
@classmethod
def f(cls) -> None:
cls.n = 1 # Error?
This should probably be okay, but type does not have attribute n, so a type checker would have to reject the code if type of cls is type. A better type would be something like Type[A] mentioned in #107. Note that we don't necessarily need to support Type[A] for user code -- this type can be internal to the type checker, similar to callable types with *args etc. that can't be represented using the type syntax.
(Mypy currently uses Any for the first argument of class methods, but that's an implementation limitation that should be fixed.)
I agree, Type[A] would be much better. To implement this, one probably needs to also infer values of variables for which type type is inferred. Then one can compare the inferred value with annotation Type[A]. By the way, currently mypy complains about this
Actually, if I replace dict with int then everything works as expected. Problem with dict probably was that it wants Dict[A,B]. This means that it should be easy to implement Type[A], mypy already can infer values of variables, so you just need to add a check that the value of cls in
deffunc(cls: Type[A]):
...
is consistent with the annotation, i.e. issubclass(cls, A) == True.
Ok, maybe it is not so easy. I just understood how huge is mypy. As a quick workaround maybe one can say that the annotation assumed for first arg in classmethod is TypeVar('', bound=type)?
Jukka, can you update the PEP to say something vague about the cls argument to class methods? Without defining something like Type[...] -- we'll open that can of worms later.
Jukka, can you update the PEP to say something vague about the cls argument to class methods? Without defining something like Type[...] -- we'll open that can of worms later.
Reply to this email directly or view it on GitHub: #123 (comment)
Activity
ilevkivskyi commentedon May 21, 2015
I agree,
Type[A]would be much better. To implement this, one probably needs to also infer values of variables for which typetypeis inferred. Then one can compare the inferred value with annotationType[A]. By the way, currently mypy complains about thisAlso, I think it would be useful to support
Type[A]for user code for the reasons mentioned in #107ilevkivskyi commentedon May 21, 2015
Actually, if I replace
dictwithintthen everything works as expected. Problem withdictprobably was that it wantsDict[A,B]. This means that it should be easy to implementType[A], mypy already can infer values of variables, so you just need to add a check that the value ofclsinis consistent with the annotation, i.e.
issubclass(cls, A) == True.ilevkivskyi commentedon May 21, 2015
Ok, maybe it is not so easy. I just understood how huge is mypy. As a quick workaround maybe one can say that the annotation assumed for first arg in classmethod is
TypeVar('', bound=type)?gvanrossum commentedon May 21, 2015
Jukka, can you update the PEP to say something vague about the
clsargument to class methods? Without defining something likeType[...]-- we'll open that can of worms later.JukkaL commentedon May 21, 2015
Ok i'll do it later today.
—
Sent from Mailbox
On Thu, May 21, 2015 at 11:16 AM, Guido van Rossum
notifications@github.com wrote: