-
Notifications
You must be signed in to change notification settings - Fork 285
Change the typing spec around string references #2144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
7627175
d08c226
070312d
41fc20c
ea921e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -222,32 +222,14 @@ String annotations | |||||||||||||
| When a type hint cannot be evaluated at runtime, that | ||||||||||||||
| definition may be expressed as a string literal, to be resolved later. | ||||||||||||||
|
|
||||||||||||||
| A situation where this occurs commonly is the definition of a | ||||||||||||||
| container class, where the class being defined occurs in the signature | ||||||||||||||
| of some of the methods. For example, the following code (the start of | ||||||||||||||
| a simple binary tree implementation) does not work:: | ||||||||||||||
|
|
||||||||||||||
| class Tree: | ||||||||||||||
| def __init__(self, left: Tree, right: Tree): | ||||||||||||||
| self.left = left | ||||||||||||||
| self.right = right | ||||||||||||||
|
|
||||||||||||||
| To address this, we write:: | ||||||||||||||
|
|
||||||||||||||
| class Tree: | ||||||||||||||
| def __init__(self, left: 'Tree', right: 'Tree'): | ||||||||||||||
| self.left = left | ||||||||||||||
| self.right = right | ||||||||||||||
|
|
||||||||||||||
| The string literal should contain a valid Python expression (i.e., | ||||||||||||||
| ``compile(lit, '', 'eval')`` should be a valid code object) and it | ||||||||||||||
| should evaluate without errors once the module has been fully loaded. | ||||||||||||||
| The local and global namespace in which it is evaluated should be the | ||||||||||||||
| same namespaces in which default arguments to the same function would | ||||||||||||||
| be evaluated. | ||||||||||||||
|
|
||||||||||||||
| Moreover, the expression should be parseable as a valid type hint, i.e., | ||||||||||||||
| it is constrained by the rules from :ref:`the expression grammar <expression-grammar>`. | ||||||||||||||
| The string literal should contain a syntactically valid Python expression | ||||||||||||||
| (i.e., ``compile(lit, '', 'eval')`` should succeed) that evaluates to a valid | ||||||||||||||
| :term:`annotation expression`. Names within the expression are looked up in the | ||||||||||||||
| same way as they would be looked up at runtime in Python 3.14 and higher if the | ||||||||||||||
| annotation was not enclosed in a string literal. Thus, name lookup follows | ||||||||||||||
|
||||||||||||||
| :term:`annotation expression`. Names within the expression are looked up in the | |
| same way as they would be looked up at runtime in Python 3.14 and higher if the | |
| annotation was not enclosed in a string literal. Thus, name lookup follows | |
| :term:`annotation expression`. Regardless of the Python version used, names within the expression are looked up in the | |
| same way as they would be looked up at runtime in Python 3.14 and higher if the | |
| annotation was not enclosed in a string literal. Thus, name lookup follows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed your change, because I believe it makes sense to specify that explicitly. If this does not meet the consensus, I will revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my understanding of the intent of the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this example would be even more illuminating if placed before
def intThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think I should move both x and y? In 3.14 it should not matter where the annotations are defined, but type checkers might still work a bit different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're aiming for this test to really clarify the behavior, I think having a version of both x and y, both before and after
def int, would be useful. Their behavior should not be the same. The stringified version should refer todef intregardless of location; the non stringified version will refer to the globalintif beforedef intUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how I understand Python 3.14 works. Maybe @JelleZijlstra can clarify. If I understand it correctly, both before and after would refer to the
def int.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 3.14 behavior would be that before or after
def int, the annotationintrefers todef int. So that should be the behavior of the stringified"int"annotation.The behavior of the unstringified
intannotation should depend on the Python version. Checking under 3.14 it should behave the same as the stringified annotation. Checking under an earlier Python, it should resolve as I described above.I don't interpret this PR to be specifying that non-stringified annotations in Python versions earlier than 3.14 should behave as if under 3.14.
So you're right -- my comment above was making an unwarranted assumption that the conformance tests specify behavior on an older version of Python. But there's no reason that should be assumed. I think this highlights an existing problem, which is that it's not clear (or documented anywhere, as far as I can find) what Python version the conformance tests should assume.
If the conformance tests should always be assumed to check under the most recent Python, then stringified and non-stringified annotations should behave the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Now I'm just not sure what to do. 😄 What would you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably for purposes of this PR the best option is to avoid checking behavior that should depend on the Python version? So that implies avoiding an unstringified
intannotation beforedef int. Which is fine -- there already isn't one. So just ignore that part of my suggestion :)I also think the conformance tests should ideally be clearer about Python version, and probably include a facility for explicitly checking some files under a specified version. But that's a bigger project, definitely out of scope for this PR.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think having
z: "int"beforedef int, as well as thex: "int"after (my original suggestion in this thread) is useful, and doesn't depend on Python version.It's the expanded idea of also having
foo: intbeforedef intthat would depend on Python version, and thus isn't worth doing here.