8000 bpo-38605: Make postponed evaluation of annotations default by isidentical · Pull Request #20434 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-38605: Make postponed evaluation of annotations default #20434

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

Merged
merged 18 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support double-stringified annotations in dataclasses
  • Loading branch information
isidentical committed Oct 3, 2020
commit 0a04da8a9d4bac80e0e912bbf8c62599dc3e65d1
6 changes: 6 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,12 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
# a eval() penalty for every single field of every dataclass
# that's defined. It was judged not worth it.

# Strip away the extra quotes as a result of double-stringifying when the
# 'annotations' feature became default.
if annotation.startswith(("'", '"')) and annotation.endswith(("'", '"')):
annotation = annotation[1:-1]


match = _MODULE_IDENTIFIER_RE.match(annotation)
if match:
ns = None
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,13 @@ class C:


class TestStringAnnotations(unittest.TestCase):
def test_double_stringification(self):
@dataclass
class T:
a: "typing.ClassVar[int]"

T()

def test_classvar(self):
# These tests assume that both "import typing" and "from
# typing import *" have been run in this file.
Expand Down
0