Skip to content

Warn against use of .__annotations__ (in many circumstances) #18236

Open
@JelleZijlstra

Description

@JelleZijlstra

Summary

Directly using the .__annotations__ attribute of an object is unsafe in a few circumstances:

  • On classes with custom metaclasses, various odd things can happen (https://peps.python.org/pep-0749/#pre-existing-bugs). On Python 3.14 and newer this only happens if at least one of the classes involved uses from __future__ import annotations; on earlier versions it can always happen.
  • Accessing __annotations__ on an instance of an annotated class works in released versions of Python (most of the time) but won't work in Python 3.14.
  • (Minor) Accessing __annotations__ on arbitrary type objects will raise AttributeError on static types, e.g. int.__annotations__.

However, it is always safe to access __annotations__ on function and module objects.

Therefore, it probably makes sense for Ruff to warn against any use of the .__annotations__ attribute, perhaps with an exception for objects known to be functions or modules.

Similar to #17853.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions