Skip to content

Commit 666eeda

Browse files
gh-132493: Support deferred annotations in Protocols (#132494)
1 parent 818b6a9 commit 666eeda

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/test/test_typing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,15 @@ class Commentable(Protocol):
45544554
)
45554555
self.assertIs(type(exc.__cause__), CustomError)
45564556

4557+
def test_deferred_evaluation_of_annotations(self):
4558+
class DeferredProto(Protocol):
4559+
x: DoesNotExist
4560+
self.assertEqual(get_protocol_members(DeferredProto), {"x"})
4561+
self.assertEqual(
4562+
annotationlib.get_annotations(DeferredProto, format=annotationlib.Format.STRING),
4563+
{'x': 'DoesNotExist'}
4564+
)
4565+
45574566

45584567
class GenericTests(BaseTestCase):
45594568

Lib/typing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,9 @@ def _get_protocol_attrs(cls):
18011801
for base in cls.__mro__[:-1]: # without object
18021802
if base.__name__ in {'Protocol', 'Generic'}:
18031803
continue
1804-
annotations = getattr(base, '__annotations__', {})
1804+
annotations = _lazy_annotationlib.get_annotations(
1805+
base, format=_lazy_annotationlib.Format.FORWARDREF
1806+
)
18051807
for attr in (*base.__dict__, *annotations):
18061808
if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
18071809
attrs.add(attr)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support creation of :class:`typing.Protocol` classes with annotations that
2+
cannot be resolved at class creation time.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy