@@ -1801,9 +1801,13 @@ 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 = _lazy_annotationlib .get_annotations (
1805- base , format = _lazy_annotationlib .Format .FORWARDREF
1806- )
1804+ try :
1805+ annotations = base .__annotations__
1806+ except Exception :
1807+ # Only go through annotationlib to handle deferred annotations if we need to
1808+ annotations = _lazy_annotationlib .get_annotations (
1809+ base , format = _lazy_annotationlib .Format .FORWARDREF
1810+ )
18071811 for attr in (* base .__dict__ , * annotations ):
18081812 if not attr .startswith ('_abc_' ) and attr not in EXCLUDED_ATTRIBUTES :
18091813 attrs .add (attr )
@@ -2020,14 +2024,17 @@ def _proto_hook(cls, other):
20202024 break
20212025
20222026 # ...or in annotations, if it is a sub-protocol.
2023- if (
2024- issubclass (other , Generic )
2025- and getattr (other , "_is_protocol" , False )
2026- and attr in _lazy_annotationlib .get_annotations (
2027- base , format = _lazy_annotationlib .Format .FORWARDREF
2028- )
2029- ):
2030- break
2027+ if issubclass (other , Generic ) and getattr (other , "_is_protocol" , False ):
2028+ # We avoid the slower path through annotationlib here because in most
2029+ # cases it should be unnecessary.
2030+ try :
2031+ annos = base .__annotations__
2032+ except Exception :
2033+ annos = _lazy_annotationlib .get_annotations (
2034+ base , format = _lazy_annotationlib .Format .FORWARDREF
2035+ )
2036+ if attr in annos :
2037+ break
20312038 else :
20322039 return NotImplemented
20332040 return True
0 commit comments