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
Special methods such as object.__del__ should have pre-defined types #3056
Hm, I wonder if we couldn't take the legalistic approach that mypy doesn't keep track of whether something is always define, only of its type if it is defined. After all from
Mypy doesn't check the definedness of various things because it's hard to do, not because avoiding the checks is the right thing to do. I'd rather move towards eventually catching some cases of undefined attributes rather than making things less strict.
If we'd define all dunder methods in object, then due to inheritance mypy (or users looking at the stubs) might think that x.__del__() is valid for all objects (including instances of user-defined classes), anything can be added to, anything can be indexed, etc. even though these clearly aren't true.
Activity
JelleZijlstra commentedon Mar 25, 2017
Mypy already does this for a few methods (see e.g.
check_getattr_methodin checker.py); it shouldn't be hard to add more.gvanrossum commentedon Mar 25, 2017
Isn't it just a matter of adding those special methods to the
objectclass in typeshed?nodakai commentedon Mar 29, 2017
@gvanrossum Hmm, I misunderstood they are only applicable to new-style classes but that was not the case:
So the case of
__del__is indeed just a matter of adding a new entry to typeshed.But I guess we really need built-in rules for
__setitem__and friends which have to care about type variances?gvanrossum commentedon Mar 29, 2017
Hm, mypy doesn't really understand the difference between old-style and new-style classes.
For
__setitem__, can you provide some code examples for what you're thinking of?JukkaL commentedon Mar 30, 2017
objectdoesn't define__del__, so we maybe shouldn't add that to typeshed. Otherwise code like this would generate false negatives:Also we could use
superto call__del__fromobjecteven though it doesn't exist.These are the attributes
objecthas in Python 2.7:We'd need to special case other special methods in mypy.
gvanrossum commentedon Mar 30, 2017
Hm, I wonder if we couldn't take the legalistic approach that mypy doesn't keep track of whether something is always define, only of its type if it is defined. After all from
we infer that C has an attribute bar of type int, and we don't keep track of whether it is set or not.
We could do the same for
__del__and others, so they provide constraints for subclasses (which is IIUC what's asked for in this issue).JukkaL commentedon Mar 30, 2017
Mypy doesn't check the definedness of various things because it's hard to do, not because avoiding the checks is the right thing to do. I'd rather move towards eventually catching some cases of undefined attributes rather than making things less strict.
If we'd define all dunder methods in
object, then due to inheritance mypy (or users looking at the stubs) might think thatx.__del__()is valid for all objects (including instances of user-defined classes), anything can be added to, anything can be indexed, etc. even though these clearly aren't true.