Skip to content

Conversation

@XuehaiPan
Copy link
Contributor

@XuehaiPan XuehaiPan commented Nov 23, 2024

Set correct __module__ attribute for methods of named tuple types.

Fixes #127187

@rhettinger rhettinger self-assigned this Nov 23, 2024
for method in methods:
method.__qualname__ = f'{typename}.{method.__name__}'
if module is not None:
method.__module__ = module
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to check module is a str object. In the test, the module argument can be passed with collections (an instance of ModuleType).

@rhettinger rhettinger closed this Nov 27, 2024
@XuehaiPan
Copy link
Contributor Author

XuehaiPan commented Nov 27, 2024

I think this PR is still valuable. It avoids potential bugs if there is a namedtuple_xxx module (e.g. namedtuple_tests.py in the local directory). Also, dataclasses.dataclass generates methods with appropriate __module__ set while typing.NamedTuple does not.

@XuehaiPan XuehaiPan deleted the correct-namedtuple-module branch March 29, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected __module__ attribute for NamedTupleType.__new__ method: namedtuple_{typename}

2 participants