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
Return an empty dict for any callable
  • Loading branch information
JelleZijlstra committed Apr 7, 2025
commit 963a1e8cd9212bafadcfaa019e19bd0e14e8b8a4
2 changes: 1 addition & 1 deletion Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ def get_annotations(
raise ValueError(f"Unsupported format {format!r}")

if ann is None:
if isinstance(obj, type):
if isinstance(obj, type) or callable(obj):
return {}
raise TypeError(f"{obj!r} does not have annotations")

Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ def test_no_annotations(self):
class CustomClass:
pass

class MyCallable:
def __call__(self):
pass

for format in Format:
if format == Format.VALUE_WITH_FAKE_GLOBALS:
continue
Expand All @@ -918,6 +922,13 @@ class CustomClass:
with self.assertRaises(TypeError):
annotationlib.get_annotations(obj, format=format)

# Callables and types with no annotations return an empty dict
for obj in (int, len, MyCallable()):
with self.subTest(format=format, obj=obj):
self.assertEqual(
annotationlib.get_annotations(obj, format=format), {}
)

def test_pep695_generic_class_with_future_annotations(self):
ann_module695 = inspect_stringized_annotations_pep695
A_annotations = annotationlib.get_annotations(ann_module695.A, eval_str=True)
Expand Down