Skip to content

Commit c8ef1d9

Browse files
pre-commit-ci[bot]Kunal Sali
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a0eb6ad commit c8ef1d9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

mypy/checker.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6252,17 +6252,19 @@ def conditional_callable_type_map(
62526252
proper_type = get_proper_type(current_type)
62536253
if isinstance(proper_type, AnyType):
62546254
# Narrow Any to a generic callable type to satisfy no-any-return in strict mode.
6255-
return {
6256-
expr: CallableType(
6257-
[AnyType(TypeOfAny.from_another_any, source_any=proper_type),
6258-
AnyType(TypeOfAny.from_another_any, source_any=proper_type)],
6259-
[nodes.ARG_STAR, nodes.ARG_STAR2],
6260-
[None, None],
6261-
ret_type=AnyType(TypeOfAny.from_another_any, source_any=proper_type),
6262-
fallback=self.named_type("builtins.function"),
6263-
is_ellipsis_args=True,
6264-
)
6265-
}, {}
6255+
# We use a synthesized fallback with fallback_to_any=True to preserve attribute access
6256+
# (fixing regressions in sympy, pandas, etc. where Any behavior was expected).
6257+
fallback = self.named_type("builtins.object")
6258+
if fallback.type.fullname == "builtins.object":
6259+
# Create a synthesized type that behaves like 'Any' but is an 'Instance'
6260+
# This satisfies callability checkpoints while preserving dynamic attribute access.
6261+
cdef = nodes.ClassDef("<any callable>", nodes.Block([]))
6262+
cdef._fullname = "<any callable>"
6263+
info = TypeInfo(nodes.SymbolTable(), cdef, "")
6264+
info.mro = fallback.type.mro
6265+
info.bases = fallback.type.bases
6266+
info.fallback_to_any = True
6267+
return {expr: Instance(info, [])}, {}
62666268

62676269
callables, uncallables = self.partition_by_callable(current_type, unsound_partition=False)
62686270

test-data/unit/check-callable.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ from typing import Any
188188
x = 5 # type: Any
189189

190190
if callable(x):
191-
reveal_type(x) # N: Revealed type is "def (*Any, **Any) -> Any"
191+
reveal_type(x) # N: Revealed type is "<any callable>"
192192
else:
193193
reveal_type(x) # N: Revealed type is "Any"
194194
[builtins fixtures/callable.pyi]

0 commit comments

Comments
 (0)