python / python/mypy

Dotted decorators on `TypedDict` classes fails for a certain package layout

Open
#17,653 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report, To Reproduce, & Actual Behaviour

I'm hitting a strange error in data_structures.py with the following project structure, with a potential culprit in runtime_validation.py:

Project/
└── package/
    ├── __init__.py
    ├── data_structures.py
    └── runtime_validation.py
  • Contents of data_structures.py
    import typing as tp
    import package.runtime_validation as _runtime_validation
    # from package.runtime_validation import register_runtime_validator  # Uncomment to make errors disappear
    
    deco = _runtime_validation.register_runtime_validator  # OK
    
    @_runtime_validation.register_runtime_validator  # OK
    class A: ...
    
    @_runtime_validation.register_runtime_validator  # E: Module has no attribute "register_runtime_validator"
    class TD(tp.TypedDict): ...
    
  • Contents of __init__.py
    import typing as tp
    
    RUNTIME_VALIDATORS: tp.Final = ...
    
    from package.data_structures import TD
    
  • Contents of runtime_validation.py
    import typing as tp
    
    from package import RUNTIME_VALIDATORS  # Comment out to make errors in `data_structures.py` disappear
    
    def register_runtime_validator(Type: type[tp.Any], /) -> type[tp.Any]:
        ...
        return Type
    

Expected Behavior

No issues

Your Environment

  • Mypy version used: 1.11.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the package layout and contents shown in data_structures.py, init.py, and runtime_validation.py with mypy 1.11.1. Start by tracing how the import cycle affects attribute lookup for the dotted decorator on the TypedDict class. Done means the example reports no missing-attribute error without uncommenting the direct import.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.