Skip to content

Commit

Permalink
Suggest typing.Any when using any as type (#12185)
Browse files Browse the repository at this point in the history
  • Loading branch information
siiptuo authored Feb 16, 2022
1 parent 777885f commit 554606b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ def analyze_unbound_type_without_type_info(self, t: UnboundType, sym: SymbolTabl
message = 'Variable "{}" is not valid as a type'
elif isinstance(sym.node, (SYMBOL_FUNCBASE_TYPES, Decorator)):
message = 'Function "{}" is not valid as a type'
notes.append('Perhaps you need "Callable[...]" or a callback protocol?')
if name == 'builtins.any':
notes.append('Perhaps you meant "typing.Any" instead of "any"?')
else:
notes.append('Perhaps you need "Callable[...]" or a callback protocol?')
elif isinstance(sym.node, MypyFile):
# TODO: suggest a protocol when supported.
message = 'Module "{}" is not valid as a type'
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,8 @@ def foo() -> None:
pass

reveal_type(foo()) # N: Revealed type is "None"

[case testAnyArgument]
def a(b: any): pass # E: Function "builtins.any" is not valid as a type \
# N: Perhaps you meant "typing.Any" instead of "any"?
[builtins fixtures/any.pyi]
8 changes: 8 additions & 0 deletions test-data/unit/fixtures/any.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import TypeVar, Iterable

T = TypeVar('T')

class int: pass
class str: pass

def any(i: Iterable[T]) -> bool: pass

0 comments on commit 554606b

Please sign in to comment.