Skip to content

unresolved-attribute warning for dynamic module attributes #2076

@duncanmmacleod

Description

@duncanmmacleod

Summary

Attempting to access a module-level object (attribute) that is set dynamically (e.g. with setattr or globals()) results in an unresolved-attribute warning, despite the attribute being actually resolvable.

Consider this toy example:

example.py
globals()["EXAMPLE"] = True
test_example.py
import example

if example.EXAMPLE:
    print("Example is True")
Python output
$ python test_example.py
Example is True
ty output
$ uvx ty check example.py test_example.py
error[unresolved-attribute]: Module `example` has no member `EXAMPLE`
 --> test_example.py:3:4
  |
1 | import example
2 |
3 | if example.EXAMPLE:
  |    ^^^^^^^^^^^^^^^
4 |     print("Example is True")
  |
info: rule `unresolved-attribute` is enabled by default

Found 1 diagnostic

A more realistic example (how I actually got here) is when using the astropy.constants module:

test_astropy.py
from astropy import constants
print(f"Speed of light: {constants.c.value} m/s")
Python output
$ uv run test_astropy.py
Speed of light: 299792458.0 m/s
ty output
$ uvx ty check test_astropy.py
error[unresolved-attribute]: Module `astropy.constants` has no member `c`
 --> test_astropy.py:2:26
  |
1 | from astropy import constants
2 | print(f"Speed of light: {constants.c.value} m/s")
  |                          ^^^^^^^^^^^
  |
info: rule `unresolved-attribute` is enabled by default

Found 1 diagnostic

Apologies if this is a duplicate of an existing report, I looked but didn't see anything obvious.

Version

ty 0.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsking for support or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions