admin管理员组文章数量:1023764
I want to provide type hints for the IDE tab completion (and IDE type linter) for the loop variable over Pandas' DataFrame itertuples(). I could do this with typing.NamedTuple
, but I though that collections.namedtuple
would be enough:
row: namedtuple('Pandas', ['Index', 'author_name'])
for row in authors_df.itertuples():
print(f"{row.Index=}, {row.author_name}")
The PyCharms linter does not see any problems, and I get tab completion... however, the GitHub CI Action, that runs flake8
linter, shows the following error:
src/package/source.py:728:21: F821 undefined name 'Pandas'
row: namedtuple('Pandas', ['Index', 'author_name'])
Is flake8 correct? How can I avoid this warning (if it is wrong), or how can I go about silencing it?
I want to provide type hints for the IDE tab completion (and IDE type linter) for the loop variable over Pandas' DataFrame itertuples(). I could do this with typing.NamedTuple
, but I though that collections.namedtuple
would be enough:
row: namedtuple('Pandas', ['Index', 'author_name'])
for row in authors_df.itertuples():
print(f"{row.Index=}, {row.author_name}")
The PyCharms linter does not see any problems, and I get tab completion... however, the GitHub CI Action, that runs flake8
linter, shows the following error:
src/package/source.py:728:21: F821 undefined name 'Pandas'
row: namedtuple('Pandas', ['Index', 'author_name'])
Is flake8 correct? How can I avoid this warning (if it is wrong), or how can I go about silencing it?
Share Improve this question asked Nov 18, 2024 at 18:58 Jakub NarębskiJakub Narębski 325k67 gold badges227 silver badges232 bronze badges1 Answer
Reset to default 2flake8 is correct, and an IDE which follows conventions would not recognise your given annotation to provide autocompletion hints. PyCharm has historically implemented annotation parsing before the typing ecosystem was mature, and as such it may do things which are non-conformant, left over from legacy implementations.
For the purposes of type annotations, collections.namedtuple
has a similar declaration format to typing.NamedTuple
's factory function form, which is the following:
NAME = namedtuple("NAME", [<items spec>])
# Usage
row: NAME
Where [<items spec>]
depends on whether you want your named tuple items (attributes) to be typed (typing.NamedTuple
) or not (collections.namedtuple
). A sequence of strings like ["Index", "author_name"]
implies untyped attributes, which is sufficient for just autocompletion.
The point here is that the assignment statement, NAME = namedtuple("NAME", ...)
, must be present to allow usage of NAME
inside type annotations. Type annotations have their own grammar, and a direct call expression like namedtuple("Pandas", ...)
does not conform to this grammar, and is invalid inside type annotations.
Note that the form NAME = <factory function>("NAME", ...)
is found in multiple places in Python typing.*
. Along with NamedTuple
, the other ones currently are:
NewType
ParamSpec
TypeAliasType
TypedDict
TypeVar
TypeVarTuple
Like NamedTuple
, you must provide the assignment statement for these, and cannot directly use any of these in a call expression in a type annotation.
I want to provide type hints for the IDE tab completion (and IDE type linter) for the loop variable over Pandas' DataFrame itertuples(). I could do this with typing.NamedTuple
, but I though that collections.namedtuple
would be enough:
row: namedtuple('Pandas', ['Index', 'author_name'])
for row in authors_df.itertuples():
print(f"{row.Index=}, {row.author_name}")
The PyCharms linter does not see any problems, and I get tab completion... however, the GitHub CI Action, that runs flake8
linter, shows the following error:
src/package/source.py:728:21: F821 undefined name 'Pandas'
row: namedtuple('Pandas', ['Index', 'author_name'])
Is flake8 correct? How can I avoid this warning (if it is wrong), or how can I go about silencing it?
I want to provide type hints for the IDE tab completion (and IDE type linter) for the loop variable over Pandas' DataFrame itertuples(). I could do this with typing.NamedTuple
, but I though that collections.namedtuple
would be enough:
row: namedtuple('Pandas', ['Index', 'author_name'])
for row in authors_df.itertuples():
print(f"{row.Index=}, {row.author_name}")
The PyCharms linter does not see any problems, and I get tab completion... however, the GitHub CI Action, that runs flake8
linter, shows the following error:
src/package/source.py:728:21: F821 undefined name 'Pandas'
row: namedtuple('Pandas', ['Index', 'author_name'])
Is flake8 correct? How can I avoid this warning (if it is wrong), or how can I go about silencing it?
Share Improve this question asked Nov 18, 2024 at 18:58 Jakub NarębskiJakub Narębski 325k67 gold badges227 silver badges232 bronze badges1 Answer
Reset to default 2flake8 is correct, and an IDE which follows conventions would not recognise your given annotation to provide autocompletion hints. PyCharm has historically implemented annotation parsing before the typing ecosystem was mature, and as such it may do things which are non-conformant, left over from legacy implementations.
For the purposes of type annotations, collections.namedtuple
has a similar declaration format to typing.NamedTuple
's factory function form, which is the following:
NAME = namedtuple("NAME", [<items spec>])
# Usage
row: NAME
Where [<items spec>]
depends on whether you want your named tuple items (attributes) to be typed (typing.NamedTuple
) or not (collections.namedtuple
). A sequence of strings like ["Index", "author_name"]
implies untyped attributes, which is sufficient for just autocompletion.
The point here is that the assignment statement, NAME = namedtuple("NAME", ...)
, must be present to allow usage of NAME
inside type annotations. Type annotations have their own grammar, and a direct call expression like namedtuple("Pandas", ...)
does not conform to this grammar, and is invalid inside type annotations.
Note that the form NAME = <factory function>("NAME", ...)
is found in multiple places in Python typing.*
. Along with NamedTuple
, the other ones currently are:
NewType
ParamSpec
TypeAliasType
TypedDict
TypeVar
TypeVarTuple
Like NamedTuple
, you must provide the assignment statement for these, and cannot directly use any of these in a call expression in a type annotation.
版权声明:本文标题:python - flake8 warning for type hints using collections.namedtuple for pandas `itertuples()` - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600358a2158420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论