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
I am writing a descriptor and attempting to annotate using the typing module. I think I have found a bug, but since I am pretty new to mypy I am posting it here. Below is the behavior I encountered:
# test_file.pyfromcollectionsimportnamedtupleclassSnapshot:
defcreate_tuple_type(self, name: str):
self.tuple_type=namedtuple(name, []) # line 6defcreate_tuple_type_1(name: str):
x=namedtuple(name, []) # line 9defcreate_tuple_type_2(name: str):
returnnamedtuple(name, [])
running: mypy test_file.py prints:
snapshot\tinutest.py:6: error: NamedTuple type as an attribute is not supported
snapshot\tinutest.py:6: error: namedtuple() expects a string literal as the first argument
snapshot\tinutest.py:9: error: namedtuple() expects a string literal as the first argument
Found 3 errors in 1 file (checked 1 source file)
I am confused:
what does the first error mean, I am not using NamedTuple (also NamedTuple is not a type, but a function)?
I explicitly set name to be a string, why the second and third error (it says 'literal', but there is no reason this would not work, it actually does in practice ;-)?
why does create_tuple_type_1 give an error, while create_tuple_type_2 doesn't?
This might more issues, but since they occur so close together, they might be correlated?
The text was updated successfully, but these errors were encountered:
The general explanation is that mypy doesn't generally support dynamic creation of types (and namedtuples are types, even if they are created through a function). For every namedtuple class, it wants to be able to figure out the name and fields at compile time.
It's unfortunate that the error message says "NamedTuple" instead of "namedtuple". typing.NamedTuple is the typing-friendly version of collections.namedtuple, but ideally mypy should put the right spelling in its error messages.
I am writing a descriptor and attempting to annotate using the typing module. I think I have found a bug, but since I am pretty new to mypy I am posting it here. Below is the behavior I encountered:
running:
mypy test_file.py
prints:I am confused:
name
to be a string, why the second and third error (it says 'literal', but there is no reason this would not work, it actually does in practice ;-)?create_tuple_type_1
give an error, whilecreate_tuple_type_2
doesn't?This might more issues, but since they occur so close together, they might be correlated?
The text was updated successfully, but these errors were encountered: