Skip to content

Change the typing spec around string references - #2144

Open
davidhalter wants to merge 27 commits into
python:mainfrom
davidhalter:string-annotations
Open

davidhalter wants to merge 27 commits into
python:mainfrom
davidhalter:string-annotations

Conversation

@davidhalter

@davidhalter davidhalter commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

I added this after the discussion here: https://discuss.python.org/t/annotation-string-references-in-class-scope-in-conformance-tests/105439

I'm not 100% sure about the wording, but I hope the direction is fine. I would like to gather some feedback before presenting this to the typing council.

Please also merge #2139 before this pull request. Otherwise it will be very hard to update Zuban's conformance test results in this pull request.

@davidhalter
davidhalter marked this pull request as ready for review January 4, 2026 23:55
@davidhalter
davidhalter marked this pull request as draft January 4, 2026 23:55
@davidhalter

Copy link
Copy Markdown
Collaborator Author

@JelleZijlstra Could you please pre-review this? What do you think about this spec change?

Comment thread docs/spec/annotations.rst Outdated
@srittau srittau added the topic: typing spec For improving the typing spec label Jan 5, 2026
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/tests/annotations_forward_refs.py Outdated
@davidhalter

Copy link
Copy Markdown
Collaborator Author

I think I have integrated all the changes. Is it time to open an issue on the Typing Council’s issue tracker asking for a decision?

Comment thread docs/spec/annotations.rst Outdated

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One wording nit, one formatting nit, and one conformance suite nit :) But overall this looks great to me.

Comment thread docs/spec/annotations.rst Outdated
Comment thread docs/spec/annotations.rst Outdated

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - much more consistent and clearly specified than before

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks @davidhalter for getting this clarified.

@davidhalter

Copy link
Copy Markdown
Collaborator Author

I have integrated all of Carl's suggestions. I will update the conformance tests as soon as the typing council approves this change. If I update it now we probably just run into merge conflicts, since especially pyrefly changes a lot.

@carljm Please let me know if you think something needs more work.

@zzzeek

zzzeek commented Mar 12, 2026

Copy link
Copy Markdown

Hi, can someone explain the intent of this change to me?

Given, under python 3.14:

Python 3.14.0 (main, Oct 20 2025, 16:44:45) [GCC 14.3.1 20250808 (Red Hat 14.3.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class A: pass
... 
>>> class X:
...     def A(self) -> "A": pass
...     
>>> class Y:
...     def A(self) -> A: pass
...     
>>> typing.get_type_hints(X.A)
{'return': <class '__main__.A'>}
>>> typing.get_type_hints(Y.A)
{'return': <function Y.A at 0x7f2052bc3c10>}
>>> 

does this change propose that it would be impossible for get_type_hints(X.A) to return class A under any circumstances, even with the quotes?

@JelleZijlstra

Copy link
Copy Markdown
Member

Yes

@zzzeek

zzzeek commented Mar 12, 2026

Copy link
Copy Markdown

are you going to change the behavior of get_type_hints() ? is this a 3.15 change? is there a pep? it should be apparent that this is an enormous backwards-incompatible change I hope?

edit: the pep is pep-749

@zzzeek

zzzeek commented Mar 12, 2026

Copy link
Copy Markdown

also is this change intended to take place regardless of whether a file is in pep-563 mode or pep-649 mode?

@rchen152

Copy link
Copy Markdown
Collaborator

I prefer "simple", although I'd also be okay with "compat". Given the concerns Carl raised, I was going to try implementing it in Pyrefly to make sure I didn't run into any showstopping issues before offering an opinion, but if it's already how ty works, I'm much less worried on that front.

(I'll still implement it in Pyrefly as soon as I can and report back if I run into unanticipated problems, but I don't think we need to wait on that.)

@davidhalter
davidhalter marked this pull request as ready for review August 31, 2026 19:41
@davidhalter

Copy link
Copy Markdown
Collaborator Author

I ran the tests with all the type checkers and added notes.

@davidhalter

Copy link
Copy Markdown
Collaborator Author

@carljm , I think you're the only one who has not ticked off python/typing-council#51.

I guess that there won't be more feedback here, so I feel like if you're ok with this change, we should be able to merge.

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did another review pass on this.

Comment thread docs/spec/annotations.rst
Comment thread conformance/tests/annotations_forward_refs.py
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/results/mypy/annotations_forward_refs.toml Outdated
Comment thread docs/spec/annotations.rst Outdated
Comment thread conformance/tests/annotations_forward_refs.py Outdated
Comment thread conformance/results/mypy/annotations_forward_refs.toml Outdated
Comment thread conformance/tests/annotations_forward_refs.py

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple remaining nits, but looks great, thank you!

Comment thread conformance/tests/annotations_future_import.py
Comment thread conformance/results/pyrefly/annotations_future_import.toml Outdated
Comment thread docs/spec/annotations.rst Outdated
@carljm
carljm dismissed erictraut’s stale review September 14, 2026 18:17

the only comment that came with this review has been addressed

@davidhalter

davidhalter commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

Ok now it should be ready. At least now this file shows how different the type checkers think about forward references: None of the type checkers agree on the errors 😄

There are probably a few new notes that might not be great. Will merge after a last OK from Carl.

Thanks for all the feedback!

@@ -0,0 +1,26 @@
conformant = "Partial"
notes = """
Names in annotations that refer to definitions after them are not resolved in the precence of `from __future__ import annotations`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we apply the same wording correction here as in the Pyrefly notes: “Forward references to nested classes are not resolved”? The module-level ClassA and ClassC forward references succeed, so this still overstates the limitation. (Also, "precence" is currently mis-spelled.)

@@ -0,0 +1,21 @@
conformant = "Partial"
notes = """
Forward references to nested classes are not resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a short note covering the newly exposed shadowing failures? The results now record missing errors for str: str = "" and z: int = 0 before def int (lines 37 and 39), but the notes only describe nested-class failures. Something like “Resolves some class annotations to outer names instead of shadowing class bindings” would cover those cases. Zuban's notes have the same omission.

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

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.