Skip to content
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]


Comment thread
isidentical marked this conversation as resolved.
Outdated
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()
Comment thread
isidentical marked this conversation as resolved.
Outdated

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