Subclass that overrides __new__
should construct instances having revealed type of subclass (not superclass)
#2510
Labels
addressed in next version
Issue is fixed and will appear in next published version
bug
Something isn't working
Description
When a subclass's
__new__
returns the result ofsuper().__new__
, the revealed type of an object returned by the subclass' constructor is that of the superclass. I expect the revealed type to be that of the subclass.Example
At my workplace, we subclass
collection.namedtuple
in order to define some defaults handling, arg validation and behavior associated with an immutable structure (we'd probably use a different approach today, sure; this was originally written using Python 2.7). Here's a demonstrative snippet:VS Code extension or command-line
I'm running Pylance
v2021.10.3
, which according to theirCHANGELOG.md
includes Pyright1.1.182
.Additional context
Since the main reason to override
__new__
is to do custom things with immutable objects, there's a decent amount of discussion online about namedtuples specifically - but they seem to fall into two buckets: discussions that predate type annotations that do talk about overriding__new__
(with examples usingcollections.namedtuple
that look very like mine) but say nothing about types, and discussions that postdate type annotations but that only mentiontyping.NamedTuple
, which (since that's a very different animal) don't usually apply very much to my situation.If this isn't actually a bug and I'm holding it wrong, I would appreciate any tips about what I could do differently to address this kind of error, which is currently happening all over the place in my codebase:
The text was updated successfully, but these errors were encountered: