| [case testSuggestCallsites1] |
| # suggest: --callsites foo.foo |
| [file foo.py] |
| def foo(arg): |
| return 12 |
| var = 0 |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| foo(arg='xyz') |
| args = [''] |
| foo(*args) |
| kwds = {'arg': ''} |
| foo(**kwds) |
| [builtins fixtures/dict.pyi] |
| [out] |
| bar.py:3: (str) |
| bar.py:4: (arg=str) |
| bar.py:6: (*typing.List[str]) |
| bar.py:8: (**typing.Dict[str, str]) |
| == |
| |
| [case testSuggestCallsitesStep2] |
| # suggest2: --callsites foo.foo |
| [file foo.py] |
| def foo(arg): |
| return 12 |
| var = 0 |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| foo(arg='xyz') |
| args = [''] |
| foo(*args) |
| kwds = {'arg': ''} |
| foo(**kwds) |
| [builtins fixtures/dict.pyi] |
| [out] |
| == |
| bar.py:3: (str) |
| bar.py:4: (arg=str) |
| bar.py:6: (*typing.List[str]) |
| bar.py:8: (**typing.Dict[str, str]) |
| |
| [case testSuggestInferFunc1] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| def foo(arg, lol=None): |
| if isinstance(arg, int): |
| arg+1 |
| else: |
| assert arg |
| arg+'1' |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| foo(lol=10, arg=10) |
| foo(None) |
| def untyped(x) -> None: |
| foo(x) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (Union[str, int, None], Optional[int]) -> None |
| == |
| |
| [case testSuggestInferFunc2] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| def foo(arg): |
| return arg |
| [file bar.py] |
| from foo import foo |
| def thing() -> str: |
| return '' |
| def bar() -> None: |
| # We stick it in a list so that the argument type is marked as "inferred", |
| # which we want to make sure doesn't show up. |
| x = ['hello'] |
| foo(x[0]) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestInferFuncAny1] |
| # suggest: foo.foo |
| # suggest: foo.bar |
| [file foo.py] |
| def foo(arg): |
| return arg.x |
| def bar(arg): |
| pass |
| [file bar.py] |
| from foo import bar |
| bar(None) |
| [out] |
| (Any) -> Any |
| (Optional[Any]) -> None |
| == |
| |
| [case testSuggestInferFuncAny2] |
| # suggest: --no-any foo.foo |
| # suggest: --no-any foo.bar |
| [file foo.py] |
| def foo(arg): |
| return arg.x |
| def bar(arg): |
| pass |
| [file bar.py] |
| from foo import bar |
| bar(None) |
| [out] |
| No guesses that match criteria! |
| No guesses that match criteria! |
| == |
| |
| [case testSuggestInferTuple] |
| # suggest: --no-any foo.foo |
| [file foo.py] |
| def foo(): |
| return 1, "1" |
| [out] |
| () -> Tuple[int, str] |
| == |
| |
| [case testSuggestInferNamedTuple] |
| # suggest: foo.foo |
| [file foo.py] |
| from typing import NamedTuple |
| N = NamedTuple('N', [('x', int)]) |
| def foo(): |
| return N(1) |
| [out] |
| () -> foo.N |
| == |
| |
| [case testSuggestInferTypedDict] |
| # suggest: foo.foo |
| [file foo.py] |
| from typing_extensions import TypedDict |
| TD = TypedDict('TD', {'x': int}) |
| def foo(): |
| return bar() |
| |
| def bar() -> TD: ... |
| [builtins fixtures/dict.pyi] |
| [out] |
| () -> foo.TD |
| == |
| |
| [case testSuggestReexportNaming] |
| # suggest: foo.foo |
| [file foo.py] |
| from bar import A |
| def foo(x): |
| return A(), A.C() |
| [file bar.py] |
| from baz import A |
| [file baz.py] |
| class A: |
| class C: |
| ... |
| class B: ... |
| |
| [file caller.py] |
| from foo import foo |
| from baz import B |
| foo(B()) |
| |
| [out] |
| (baz.B) -> Tuple[foo.A, foo:A.C] |
| == |
| |
| [case testSuggestInferInit] |
| # suggest: foo.Foo.__init__ |
| [file foo.py] |
| class Foo: |
| def __init__(self, arg): |
| self.arg = arg |
| [file bar.py] |
| from foo import Foo |
| Foo('lol') |
| [out] |
| (str) -> None |
| == |
| |
| [case testSuggestTryText] |
| # flags: --py2 |
| # suggest: --try-text foo.foo |
| [file foo.py] |
| def foo(s): |
| return s |
| [file bar.py] |
| from foo import foo |
| foo('lol') |
| [out] |
| (Text) -> Text |
| == |
| |
| [case testSuggestInferMethod1] |
| # flags: --strict-optional |
| # suggest: --no-any foo.Foo.foo |
| [file foo.py] |
| class Foo: |
| def __init__(self) -> None: |
| self.y = '10' |
| |
| def foo(self, arg, lol=None): |
| if isinstance(arg, int): |
| return arg+1 |
| else: |
| assert arg |
| return arg+self.y |
| [file bar.py] |
| from foo import Foo |
| def bar() -> None: |
| x = Foo() |
| x.foo('abc') |
| x.foo(lol=10, arg=10) |
| x.foo(None) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (Union[str, int, None], Optional[int]) -> object |
| == |
| |
| [case testSuggestInferMethod2] |
| # flags: --strict-optional |
| # suggest: foo.Foo.foo |
| [file foo.py] |
| class Foo: |
| def i(self, x: int) -> int: return x |
| def s(self, x: str) -> str: return x |
| |
| def foo(self, arg, lol=None): |
| if isinstance(arg, int): |
| return self.i(arg) |
| else: |
| assert arg |
| return self.s(arg) |
| [file bar.py] |
| from typing import Union |
| from foo import Foo |
| def bar() -> None: |
| x = Foo() |
| x.foo('abc') |
| x.foo(lol=10, arg=10) |
| a: Union[str, int] = x.foo(None) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (Union[str, int, None], Optional[int]) -> Union[int, str] |
| == |
| |
| [case testSuggestInferMethod3] |
| # flags: --strict-optional |
| # suggest2: foo.Foo.foo |
| [file foo.py] |
| class Foo: |
| def foo(self, lol = None): |
| pass |
| |
| def lol(self) -> None: |
| self.foo('lol') |
| [file bar.py] |
| from foo import Foo |
| def bar() -> None: |
| x = Foo() |
| x.foo('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| == |
| (Optional[str]) -> None |
| |
| [case testSuggestInferFunctionUnreachable] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| import sys |
| def foo(lol): |
| if sys.platform == 'nothing': |
| return lol |
| else: |
| return lol + lol |
| [file bar.py] |
| from foo import foo |
| foo('test') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestInferMethodStep2] |
| # flags: --strict-optional |
| # suggest2: foo.Foo.foo |
| [file foo.py] |
| class Foo: |
| def i(self, x: int) -> int: return x |
| def s(self, x: str) -> str: return x |
| |
| def foo(self, arg, lol=None): |
| if isinstance(arg, int): |
| return self.i(arg) |
| else: |
| assert arg |
| return self.s(arg) |
| [file bar.py] |
| from typing import Union |
| from foo import Foo |
| def bar() -> None: |
| x = Foo() |
| x.foo('abc') |
| x.foo(lol=10, arg=10) |
| a: Union[str, int] = x.foo(None) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| == |
| (Union[str, int, None], Optional[int]) -> Union[int, str] |
| |
| [case testSuggestInferNestedMethod] |
| # flags: --strict-optional |
| # suggest: foo.Foo.Bar.baz |
| [file foo.py] |
| class Foo: |
| class Bar: |
| def baz(self, lol): |
| return lol |
| [file bar.py] |
| from foo import Foo |
| def bar() -> None: |
| x = Foo.Bar() |
| x.baz('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestCallable] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| # suggest: foo.bar |
| # suggest: --flex-any=0.9 foo.bar |
| # suggest: foo.baz |
| # suggest: foo.quux |
| [file foo.py] |
| def foo(f): |
| return f(0, "lol") |
| def bar(f): |
| return f(0, "lol", 100) |
| def baz(f): |
| return f(y=1) + f(x=10, y=1) |
| def quux(f): |
| return f(1) |
| [file bar.py] |
| from typing import Any |
| from foo import foo, bar, baz, quux |
| |
| def whatever(x: int, y: str) -> int: |
| return 0 |
| |
| def starargs(*args: Any) -> int: |
| return 0 |
| |
| def named(*, x: int = 0, y: int) -> str: |
| return '0' |
| |
| # we don't properly handle default really. we just assume it is |
| # actually required. |
| def default(x: int = 0) -> str: |
| return '0' |
| |
| def test() -> None: |
| foo(whatever) |
| bar(starargs) |
| baz(named) |
| quux(default) |
| [out] |
| (Callable[[int, str], int]) -> int |
| (Callable[..., int]) -> int |
| No guesses that match criteria! |
| (Callable[..., str]) -> str |
| (Callable[[int], str]) -> str |
| == |
| |
| [case testSuggestNewSemanal] |
| # flags: --strict-optional |
| # suggest: foo.Foo.foo |
| # suggest: foo.foo |
| [file foo.py] |
| class Foo: |
| def __init__(self) -> None: |
| self.y = '10' |
| |
| def foo(self, arg, lol=None): |
| if isinstance(arg, int): |
| return arg+1 |
| else: |
| assert arg |
| return arg+self.y |
| def foo(arg, lol=None): |
| if isinstance(arg, int): |
| arg+1 |
| else: |
| assert arg |
| arg+'1' |
| [file bar.py] |
| from foo import Foo, foo |
| def bar() -> None: |
| x = Foo() |
| x.foo('abc') |
| x.foo(lol=10, arg=10) |
| x.foo(None) |
| def baz() -> None: |
| foo('abc') |
| foo(lol=10, arg=10) |
| foo(None) |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (Union[str, int, None], Optional[int]) -> object |
| (Union[str, int, None], Optional[int]) -> None |
| == |
| |
| [case testSuggestInferFuncDecorator1] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| from typing import TypeVar |
| F = TypeVar('F') |
| |
| def dec(x: F) -> F: |
| return x |
| |
| @dec |
| def foo(arg): |
| return arg |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestInferFuncDecorator2] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| from typing import TypeVar, Callable, Any |
| F = TypeVar('F', bound=Callable[..., Any]) |
| |
| def dec(x: F) -> F: |
| return x |
| |
| @dec |
| def foo(arg): |
| return arg |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestInferFuncDecorator3] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file foo.py] |
| from typing import TypeVar, Callable, Any |
| F = TypeVar('F', bound=Callable[..., Any]) |
| |
| def dec(s: str) -> Callable[[F], F]: |
| def f(x: F) -> F: |
| return x |
| return f |
| |
| @dec('lol') |
| def foo(arg): |
| return arg |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestInferFuncDecorator4] |
| # flags: --strict-optional |
| # suggest: foo.foo |
| [file dec.py] |
| from typing import TypeVar, Callable, Any |
| F = TypeVar('F', bound=Callable[..., Any]) |
| |
| def dec(s: str) -> Callable[[F], F]: |
| def f(x: F) -> F: |
| return x |
| return f |
| |
| [file foo.py] |
| import dec |
| |
| @dec.dec('lol') |
| def foo(arg): |
| return arg |
| [file bar.py] |
| from foo import foo |
| def bar() -> None: |
| foo('abc') |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (str) -> str |
| == |
| |
| [case testSuggestFlexAny1] |
| # flags: --strict-optional |
| # suggest: --flex-any=0.4 m.foo |
| # suggest: --flex-any=0.7 m.foo |
| # suggest: --flex-any=0.4 m.bar |
| # suggest: --flex-any=0.6 m.bar |
| # suggest2: --flex-any=0.4 m.foo |
| # suggest2: --flex-any=0.7 m.foo |
| [file m.py] |
| from typing import Any |
| any: Any |
| |
| def foo(arg): |
| return 0 |
| def bar(x, y): |
| return any |
| |
| [file n.py] |
| from typing import Any |
| any: Any |
| |
| from m import foo, bar |
| def wtvr() -> None: |
| foo(any) |
| bar(1, 2) |
| |
| [file n.py.2] |
| from typing import Any |
| any: Any |
| |
| from m import foo, bar |
| def wtvr() -> None: |
| foo([any]) |
| |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| (Any) -> int |
| No guesses that match criteria! |
| (int, int) -> Any |
| No guesses that match criteria! |
| == |
| (typing.List[Any]) -> int |
| (typing.List[Any]) -> int |
| |
| |
| [case testSuggestFlexAny2] |
| # flags: --strict-optional |
| # suggest: --flex-any=0.5 m.baz |
| # suggest: --flex-any=0.0 m.baz |
| # suggest: --flex-any=0.5 m.F.foo |
| # suggest: --flex-any=0.7 m.F.foo |
| # suggest: --flex-any=0.7 m.noargs |
| [file m.py] |
| # Test mostly corner cases |
| |
| # Test that a None return doesn't get counted |
| def baz(x): |
| pass |
| |
| class F: |
| # Test that self doesn't get counted |
| def foo(self, x): |
| return 0 |
| |
| # Make sure we don't crash on noarg functions |
| def noargs(): |
| pass |
| |
| [builtins fixtures/isinstancelist.pyi] |
| [out] |
| No guesses that match criteria! |
| (Any) -> None |
| (Any) -> int |
| No guesses that match criteria! |
| () -> None |
| == |
| |
| [case testSuggestInferClassMethod] |
| # flags: --strict-optional |
| # suggest: foo.F.bar |
| # suggest: foo.F.baz |
| [file foo.py] |
| class F: |
| @classmethod |
| def bar(cls, x, y): |
| return x |
| |
| @staticmethod |
| def baz(x, y): |
| return x |
| |
| [file bar.py] |
| from foo import F |
| def bar(iany) -> None: |
| F.bar(0, iany) |
| F().bar(0, 5) |
| F.baz('lol', iany) |
| F().baz('lol', 10) |
| [builtins fixtures/classmethod.pyi] |
| [out] |
| (int, int) -> int |
| (str, int) -> str |
| == |
| |
| [case testSuggestColonBasic] |
| # suggest: tmp/foo.py:1 |
| # suggest: tmp/bar/baz.py:2 |
| [file foo.py] |
| def func(arg): |
| return 0 |
| func('test') |
| from bar.baz import C |
| C().method('test') |
| [file bar/__init__.py] |
| [file bar/baz.py] |
| class C: |
| def method(self, x): |
| return 0 |
| [out] |
| (str) -> int |
| (str) -> int |
| == |
| |
| [case testSuggestColonBadLocation] |
| # suggest: tmp/foo.py:7:8:9 |
| [file foo.py] |
| [out] |
| Malformed location for function: tmp/foo.py:7:8:9. Must be either package.module.Class.method or path/to/file.py:line |
| == |
| |
| [case testSuggestColonBadLine] |
| # suggest: tmp/foo.py:bad |
| [file foo.py] |
| [out] |
| Line number must be a number. Got bad |
| == |
| |
| [case testSuggestColonBadFile] |
| # suggest: tmp/foo.txt:1 |
| [file foo.txt] |
| def f(): pass |
| [out] |
| Source file is not a Python file |
| == |
| |
| [case testSuggestColonUnknownLine] |
| # suggest: tmp/foo.py:42 |
| [file foo.py] |
| def func(x): |
| return 0 |
| func('test') |
| [out] |
| Cannot find a function at line 42 |
| == |
| |
| [case testSuggestColonClass] |
| # suggest: tmp/foo.py:1 |
| [file foo.py] |
| class C: |
| pass |
| [out] |
| Cannot find a function at line 1 |
| == |
| |
| [case testSuggestColonDecorator] |
| # suggest: tmp/foo.py:6 |
| [file foo.py] |
| from typing import TypeVar, Callable, Any |
| F = TypeVar('F', bound=Callable[..., Any]) |
| def deco(f: F) -> F: ... |
| |
| @deco |
| def func(arg): |
| return 0 |
| func('test') |
| [out] |
| (str) -> int |
| == |
| |
| [case testSuggestColonMethod] |
| # suggest: tmp/foo.py:3 |
| [file foo.py] |
| class Out: |
| class In: |
| def method(self, x): |
| return Out() |
| x: Out.In |
| x.method(x) |
| [out] |
| (foo:Out.In) -> foo.Out |
| == |
| |
| [case testSuggestColonMethodJSON] |
| # suggest: --json tmp/foo.py:3 |
| [file foo.py] |
| class Out: |
| class In: |
| def method(self, x): |
| return Out() |
| x: Out.In |
| x.method(x) |
| [out] |
| \[{"func_name": "Out.In.method", "line": 3, "path": "tmp/foo.py", "samples": 0, "signature": {"arg_types": ["foo:Out.In"], "return_type": "foo.Out"}}] |
| == |
| |
| [case testSuggestColonNonPackageDir] |
| # cmd: mypy foo/bar/baz.py |
| # suggest: tmp/foo/bar/baz.py:1 |
| [file foo/bar/baz.py] |
| def func(arg): |
| return 0 |
| func('test') |
| [out] |
| (str) -> int |
| == |