-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
Description
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.py
from collections import namedtuple
class Snapshot:
def create_tuple_type(self, name: str):
self.tuple_type = namedtuple(name, []) # line 6
def create_tuple_type_1(name: str):
x = namedtuple(name, []) # line 9
def create_tuple_type_2(name: str):
return namedtuple(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, whilecreate_tuple_type_2
doesn't?
This might more issues, but since they occur so close together, they might be correlated?
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
Select code repository
Activity
JelleZijlstra commentedon Aug 9, 2021
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 ofcollections.namedtuple
, but ideally mypy should put the right spelling in its error messages.gemerden commentedon Aug 12, 2021
If it doesn't support dynamic type creation, shouldn't it ignore it?
namedtuple() expects a string literal as the first argument
, this is just not true, andcollections.namedtuple
is not annotated.I mean, this still seems very odd to me.