Skip to content

Fix ClassVar forward reference detection - #1593

Open
wanxiankai wants to merge 2 commits into
python-attrs:mainfrom
wanxiankai:fix/classvar-forwardref
Open

Fix ClassVar forward reference detection#1593
wanxiankai wants to merge 2 commits into
python-attrs:mainfrom
wanxiankai:fix/classvar-forwardref

Conversation

@wanxiankai

Copy link
Copy Markdown

Summary

Fixes #1575.

_is_class_var() now unwraps ForwardRef.__forward_arg__ before applying the existing string-prefix checks.
This lets attrs recognize Python 3.14-style forward references such as ForwardRef("ClassVar[str]", is_class=True) as class variables when ClassVar was only imported under TYPE_CHECKING.

Tests cover both the direct _is_class_var() case and the Python 3.14 attrs.define behavior from the issue.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing-examples/baseline.py or, if necessary, typing-examples/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      The next version is the second number in the current release + 1.
      The first number represents the current year.
      So if the current version on PyPI is 26.2.0, the next version is gonna be 26.3.0.
      If the next version is the first in the new year, it'll be 27.1.0.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

@wanxiankai
wanxiankai force-pushed the fix/classvar-forwardref branch from d1d8e8d to c74040f Compare July 19, 2026 07:11
@codspeed-hq

codspeed-hq Bot commented Jul 31, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 61.12%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks
✅ 15 untouched benchmarks
⏩ 2 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime import attrs 49.2 ms 127 ms -61.28%
WallTime import attr 48.7 ms 124.9 ms -60.96%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wanxiankai:fix/classvar-forwardref (8328ba7) with main (c27ba27)2

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (ca0f045) during the generation of this report, so c27ba27 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

attrs doesn't recognize ClassVar if not imported in Python 3.14

2 participants