-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subclasses of NamedTuple #3739
Comments
It seems to work if you use from typing import NamedTuple
_Foo = NamedTuple('_Foo', [
('bar', str),
('baz', str),
])
class Foo(_Foo):
def __new__(cls, bar, baz='z') -> 'Foo':
return super().__new__(cls, bar, baz) OTOH I'm not sure whether mypy properly typechecks namedtuples unless they subclass directly from Lines 890 to 906 in 8709da5
|
I didn't know I could use I replaced
|
@progval from typing import NamedTuple
class Link(NamedTuple):
"""Represent a link between two nodes."""
start: int
end: int
label: str = '<no name>'
def weight(self) -> float:
return abs(self.end - self.start)/2 |
Since you're on python3, you might want to use the syntax instead, like this:
I use this in my project and mypy deals with it just fine. It's also much more readable. |
Concerning |
Hi,
I often use a subclass to a namedtuple to add optional arguments (and/or methods) to a namedtuple.
For instance, this gives this kind of code:
which I think is completely valid Python.
However, Mypy raises the following errors:
Is there something I am doing wrong?
Thanks!
The text was updated successfully, but these errors were encountered: