Skip to content

Unexpected breakage in namedtuple() handling #2622

@grievejia

Description

@grievejia

Describe the Bug

When import collections.abc appears alongside import collections, pyrefly loses its special handling of collections.namedtuple() and resolves the constructor to tuple.__new__. This causes unexpected-keyword errors when constructing namedtuple instances with keyword arguments.

Minimal repro:

import collections
import collections.abc  # This import triggers the bug

GetWorkResponse = collections.namedtuple(
    "GetWorkResponse",
    (
        "task_id",
    ),
)

response = GetWorkResponse(
    task_id="abc123",  # pyrefly: unexpected-keyword in tuple.__new__
)
print(response.task_id)  # pyrefly: Object of class `tuple` has no attribute `task_id` 

One theory: the submodule import collections.abc may have shadowed or interfered with pyrefly's resolution of the collections module, causing collections.namedtuple to no longer be recognized as the namedtuple factory.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABAMa5SwsMS7pwA66arUat2nbrziFU2FkyYBiJgBUAFhDhNh9ZgzoQA5oZh0tDNTCbYArocGCA4jAYB1egGsASvFp8rALxiHDBcPHyE6KhUMJgMNsSwABSCCgr8IM5unj5wfnAwGQA0qWkp6GmVTBkMqHAeAPoQmMWlCgCUJejtDuh0vlKBTFnudN4D-uWVtfVNmAEZsiwAjABMAMzFCspk-WBQpIhMNugw%2BMShDLEAtB4wpADu9JjaFfGJMIQNDacP34I9dDEAzoBhJfp5QaEGaNZrtbZMXYwfaHJgAeWwACtLkxcGBWFA6loAAbvWDEphqOpMdC4JioBj6CC2K5MUl1WGYCkgIogGzcKDSMgUZQAVQFEAYpCYYBOYSkvUwyJl9CoDJ%2BNio2FMSXwRwgoPh1wAfEw4PpEG0mP14nQKmAMgA5TXauhHYD4AC%2BGUEPJASJR0NwVCgIqYAAVSHsDmaMDgCGJ0JBDDY6AzwoRBMoAMowKxqRnEOCIAD0JYDB0I9EMJZg6BLmFwLDgJbYSaMqfTvBLKro9IAbqhoLJYInk53JBVcMRJ9JBGQLLxrv3THBwkwghl1oQ1j70CBPbzUGEVwAxaAwChoLB4IjCz1AA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions