| -- Test cases for stubgen that generate stubs from Python code |
| |
| [case testEmptyFile] |
| [out] |
| |
| [case testSingleFunction] |
| def f(): |
| x = 1 |
| [out] |
| def f() -> None: ... |
| |
| [case testTwoFunctions] |
| def f(a, b): |
| x = 1 |
| def g(arg): |
| pass |
| [out] |
| def f(a, b) -> None: ... |
| def g(arg) -> None: ... |
| |
| [case testDefaultArgInt] |
| def f(a, b=2): ... |
| def g(b=-1, c=0): ... |
| [out] |
| def f(a, b: int = ...) -> None: ... |
| def g(b: int = ..., c: int = ...) -> None: ... |
| |
| [case testDefaultArgNone] |
| def f(x=None): ... |
| [out] |
| from _typeshed import Incomplete |
| |
| def f(x: Incomplete | None = ...) -> None: ... |
| |
| [case testDefaultArgBool] |
| def f(x=True, y=False): ... |
| [out] |
| def f(x: bool = ..., y: bool = ...) -> None: ... |
| |
| [case testDefaultArgStr] |
| def f(x='foo'): ... |
| [out] |
| def f(x: str = ...) -> None: ... |
| |
| [case testDefaultArgBytes] |
| def f(x=b'foo'): ... |
| [out] |
| def f(x: bytes = ...) -> None: ... |
| |
| [case testDefaultArgFloat] |
| def f(x=1.2): ... |
| [out] |
| def f(x: float = ...) -> None: ... |
| |
| [case testDefaultArgOther] |
| def f(x=ord): ... |
| [out] |
| def f(x=...) -> None: ... |
| |
| [case testPreserveFunctionAnnotation] |
| def f(x: Foo) -> Bar: ... |
| def g(x: Foo = Foo()) -> Bar: ... |
| [out] |
| def f(x: Foo) -> Bar: ... |
| def g(x: Foo = ...) -> Bar: ... |
| |
| [case testPreserveFunctionAnnotationWithArgs] |
| def f(x: foo['x']) -> bar: ... |
| def g(x: foo[x]) -> bar: ... |
| def h(x: foo['x', 'y']) -> bar: ... |
| def i(x: foo[x, y]) -> bar: ... |
| def j(x: foo['x', y]) -> bar: ... |
| def k(x: foo[x, 'y']) -> bar: ... |
| def lit_str(x: Literal['str']) -> Literal['str']: ... |
| def lit_int(x: Literal[1]) -> Literal[1]: ... |
| [out] |
| def f(x: foo['x']) -> bar: ... |
| def g(x: foo[x]) -> bar: ... |
| def h(x: foo['x', 'y']) -> bar: ... |
| def i(x: foo[x, y]) -> bar: ... |
| def j(x: foo['x', y]) -> bar: ... |
| def k(x: foo[x, 'y']) -> bar: ... |
| def lit_str(x: Literal['str']) -> Literal['str']: ... |
| def lit_int(x: Literal[1]) -> Literal[1]: ... |
| |
| [case testPreserveVarAnnotation] |
| x: Foo |
| [out] |
| x: Foo |
| |
| [case testPreserveVarAnnotationWithoutQuotes] |
| x: 'Foo' |
| [out] |
| x: Foo |
| |
| [case testVarArgs] |
| def f(x, *y): ... |
| [out] |
| def f(x, *y) -> None: ... |
| |
| [case testKwVarArgs] |
| def f(x, **y): ... |
| [out] |
| def f(x, **y) -> None: ... |
| |
| [case testVarArgsWithKwVarArgs] |
| def f(a, *b, **c): ... |
| def g(a, *b, c=1): ... |
| def h(a, *b, c=1, **d): ... |
| def i(a, *, b=1): ... |
| def j(a, *, b=1, **c): ... |
| [out] |
| def f(a, *b, **c) -> None: ... |
| def g(a, *b, c: int = ...) -> None: ... |
| def h(a, *b, c: int = ..., **d) -> None: ... |
| def i(a, *, b: int = ...) -> None: ... |
| def j(a, *, b: int = ..., **c) -> None: ... |
| |
| [case testClass] |
| class A: |
| def f(self, x): |
| x = 1 |
| def g(): ... |
| [out] |
| class A: |
| def f(self, x) -> None: ... |
| |
| def g() -> None: ... |
| |
| [case testVariables] |
| i = 1 |
| s = 'a' |
| f = 1.5 |
| c1 = 1j |
| c2 = 0j + 1 |
| bl1 = True |
| bl2 = False |
| bts = b'' |
| [out] |
| i: int |
| s: str |
| f: float |
| c1: complex |
| c2: complex |
| bl1: bool |
| bl2: bool |
| bts: bytes |
| |
| [case testVariablesWithUnary] |
| i = +-1 |
| f = -1.5 |
| c1 = -1j |
| c2 = -1j + 1 |
| bl1 = not True |
| bl2 = not not False |
| [out] |
| i: int |
| f: float |
| c1: complex |
| c2: complex |
| bl1: bool |
| bl2: bool |
| |
| [case testVariablesWithUnaryWrong] |
| i = not +1 |
| bl1 = -True |
| bl2 = not -False |
| bl3 = -(not False) |
| [out] |
| from _typeshed import Incomplete |
| |
| i: Incomplete |
| bl1: Incomplete |
| bl2: Incomplete |
| bl3: Incomplete |
| |
| [case testAnnotatedVariable] |
| x: int = 1 |
| [out] |
| x: int |
| |
| [case testAnnotatedVariableGeneric] |
| x: Foo[int, str] = ... |
| [out] |
| x: Foo[int, str] |
| |
| [case testAnnotatedVariableOldSyntax] |
| x = 1 # type: int |
| [out] |
| x: int |
| |
| [case testAnnotatedVariableNone] |
| x: None |
| [out] |
| x: None |
| |
| [case testAnnotatedVariableNoneOldSyntax] |
| x = None # type: None |
| [out] |
| x: None |
| |
| [case testMultipleVariable] |
| x = y = 1 |
| [out] |
| x: int |
| y: int |
| |
| [case testClassVariable] |
| class C: |
| x = 1 |
| [out] |
| class C: |
| x: int |
| |
| [case testInitTypeAnnotationPreserved] |
| class C: |
| def __init__(self, x: str): |
| pass |
| [out] |
| class C: |
| def __init__(self, x: str) -> None: ... |
| |
| [case testSelfAssignment] |
| class C: |
| def __init__(self): |
| self.x = 1 |
| x.y = 2 |
| [out] |
| class C: |
| x: int |
| def __init__(self) -> None: ... |
| |
| [case testSelfAndClassBodyAssignment] |
| x = 1 |
| class C: |
| x = 1 |
| def __init__(self): |
| self.x = 1 |
| self.x = 1 |
| [out] |
| x: int |
| |
| class C: |
| x: int |
| def __init__(self) -> None: ... |
| |
| [case testEmptyClass] |
| class A: ... |
| [out] |
| class A: ... |
| |
| [case testSkipPrivateFunction] |
| def _f(): ... |
| def g(): ... |
| [out] |
| def g() -> None: ... |
| |
| [case testIncludePrivateFunction] |
| # flags: --include-private |
| def _f(): ... |
| def g(): ... |
| [out] |
| def _f() -> None: ... |
| def g() -> None: ... |
| |
| [case testSkipPrivateMethod] |
| class A: |
| def _f(self): ... |
| [out] |
| class A: ... |
| |
| [case testIncludePrivateMethod] |
| # flags: --include-private |
| class A: |
| def _f(self): ... |
| [out] |
| class A: |
| def _f(self) -> None: ... |
| |
| [case testSkipPrivateVar] |
| _x = 1 |
| class A: |
| _y = 1 |
| [out] |
| class A: ... |
| |
| [case testIncludePrivateVar] |
| # flags: --include-private |
| _x = 1 |
| class A: |
| _y = 1 |
| [out] |
| _x: int |
| |
| class A: |
| _y: int |
| |
| [case testSpecialInternalVar] |
| __all__ = [] |
| __author__ = '' |
| __version__ = '' |
| [out] |
| |
| [case testBaseClass] |
| class A: ... |
| class B(A): ... |
| [out] |
| class A: ... |
| class B(A): ... |
| |
| [case testDecoratedFunction] |
| @decorator |
| def foo(x): ... |
| [out] |
| def foo(x) -> None: ... |
| |
| [case testMultipleAssignment] |
| x, y = 1, 2 |
| [out] |
| from _typeshed import Incomplete |
| |
| x: Incomplete |
| y: Incomplete |
| |
| [case testMultipleAssignmentAnnotated] |
| x, y = 1, "2" # type: int, str |
| [out] |
| x: int |
| y: str |
| |
| [case testMultipleAssignment2] |
| [x, y] = 1, 2 |
| [out] |
| from _typeshed import Incomplete |
| |
| x: Incomplete |
| y: Incomplete |
| |
| [case testKeywordOnlyArg] |
| def f(x, *, y=1): ... |
| def g(x, *, y=1, z=2): ... |
| [out] |
| def f(x, *, y: int = ...) -> None: ... |
| def g(x, *, y: int = ..., z: int = ...) -> None: ... |
| |
| [case testProperty] |
| class A: |
| @property |
| def f(self): |
| return 1 |
| @f.setter |
| def f(self, x): ... |
| |
| def h(self): |
| self.f = 1 |
| [out] |
| class A: |
| @property |
| def f(self): ... |
| @f.setter |
| def f(self, x) -> None: ... |
| def h(self) -> None: ... |
| |
| [case testFunctoolsCachedProperty] |
| import functools |
| |
| class A: |
| @functools.cached_property |
| def x(self): |
| return 'x' |
| [out] |
| import functools |
| |
| class A: |
| @functools.cached_property |
| def x(self): ... |
| |
| [case testFunctoolsCachedPropertyAlias] |
| import functools as ft |
| |
| class A: |
| @ft.cached_property |
| def x(self): |
| return 'x' |
| [out] |
| import functools as ft |
| |
| class A: |
| @ft.cached_property |
| def x(self): ... |
| |
| [case testCachedProperty] |
| from functools import cached_property |
| |
| class A: |
| @cached_property |
| def x(self): |
| return 'x' |
| [out] |
| from functools import cached_property |
| |
| class A: |
| @cached_property |
| def x(self): ... |
| |
| [case testCachedPropertyAlias] |
| from functools import cached_property as cp |
| |
| class A: |
| @cp |
| def x(self): |
| return 'x' |
| [out] |
| from functools import cached_property as cp |
| |
| class A: |
| @cp |
| def x(self): ... |
| |
| [case testStaticMethod] |
| class A: |
| @staticmethod |
| def f(x): ... |
| [out] |
| class A: |
| @staticmethod |
| def f(x) -> None: ... |
| |
| [case testClassMethod] |
| class A: |
| @classmethod |
| def f(cls): ... |
| [out] |
| class A: |
| @classmethod |
| def f(cls) -> None: ... |
| |
| [case testIfMainCheck] |
| def a(): ... |
| if __name__ == '__main__': |
| x = 1 |
| def f(): ... |
| def b(): ... |
| [out] |
| def a() -> None: ... |
| def b() -> None: ... |
| |
| [case testImportStar] |
| from x import * |
| from a.b import * |
| def f(): ... |
| [out] |
| from x import * |
| from a.b import * |
| |
| def f() -> None: ... |
| |
| [case testNoSpacesBetweenEmptyClasses] |
| class X: |
| def g(self): ... |
| class A: ... |
| class B: ... |
| class C: |
| def f(self): ... |
| [out] |
| class X: |
| def g(self) -> None: ... |
| |
| class A: ... |
| class B: ... |
| |
| class C: |
| def f(self) -> None: ... |
| |
| [case testExceptionBaseClasses] |
| class A(Exception): ... |
| class B(ValueError): ... |
| [out] |
| class A(Exception): ... |
| class B(ValueError): ... |
| |
| [case testOmitSomeSpecialMethods] |
| class A: |
| def __str__(self): ... |
| def __repr__(self): ... |
| def __eq__(self): ... |
| def __getstate__(self): ... |
| def __setstate__(self, state): ... |
| [out] |
| class A: |
| def __eq__(self): ... |
| |
| -- Tests that will perform runtime imports of modules. |
| -- Don't use `_import` suffix if there are unquoted forward references. |
| |
| [case testOmitDefsNotInAll_import] |
| __all__ = [] + ['f'] |
| def f(): ... |
| def g(): ... |
| [out] |
| def f() -> None: ... |
| |
| [case testOmitDefsNotInAll_semanal] |
| __all__ = ['f'] |
| def f(): ... |
| def g(): ... |
| [out] |
| def f() -> None: ... |
| |
| [case testVarDefsNotInAll_import] |
| __all__ = [] + ['f', 'g'] |
| def f(): ... |
| x = 1 |
| y = 1 |
| def g(): ... |
| [out] |
| def f() -> None: ... |
| def g() -> None: ... |
| |
| [case testIncludeClassNotInAll_import] |
| __all__ = [] + ['f'] |
| def f(): ... |
| class A: ... |
| [out] |
| def f() -> None: ... |
| |
| class A: ... |
| |
| [case testAllAndClass_import] |
| __all__ = ['A'] |
| class A: |
| x = 1 |
| def f(self): ... |
| [out] |
| class A: |
| x: int |
| def f(self) -> None: ... |
| |
| [case testSkipMultiplePrivateDefs] |
| class A: ... |
| _x = 1 |
| _y = 1 |
| _z = 1 |
| class C: ... |
| [out] |
| class A: ... |
| class C: ... |
| |
| [case testIncludeMultiplePrivateDefs] |
| # flags: --include-private |
| class A: ... |
| _x = 1 |
| _y = 1 |
| _z = 1 |
| class C: ... |
| [out] |
| class A: ... |
| |
| _x: int |
| _y: int |
| _z: int |
| |
| class C: ... |
| |
| [case testIncludeFromImportIfInAll_import] |
| from re import match, search, sub |
| __all__ = ['match', 'sub', 'x'] |
| x = 1 |
| [out] |
| from re import match as match, sub as sub |
| |
| x: int |
| |
| [case testExportModule_import] |
| import re |
| __all__ = ['re', 'x'] |
| x = 1 |
| y = 2 |
| [out] |
| import re as re |
| |
| x: int |
| |
| [case testExportModule2_import] |
| import re |
| __all__ = ['re', 'x'] |
| x = 1 |
| y = 2 |
| [out] |
| import re as re |
| |
| x: int |
| |
| [case testExportModuleAs_import] |
| import re as rex |
| __all__ = ['rex', 'x'] |
| x = 1 |
| y = 2 |
| [out] |
| import re as rex |
| |
| x: int |
| |
| [case testExportModuleInPackage_import] |
| import urllib.parse as p |
| __all__ = ['p'] |
| [out] |
| import urllib.parse as p |
| |
| [case testExportPackageOfAModule_import] |
| import urllib.parse |
| __all__ = ['urllib'] |
| |
| [out] |
| import urllib as urllib |
| |
| [case testRelativeImportAll] |
| from .x import * |
| [out] |
| from .x import * |
| |
| [case testCommentForUndefinedName_import] |
| __all__ = ['f', 'x', 'C', 'g'] |
| def f(): ... |
| x = 1 |
| class C: |
| def g(self): ... |
| [out] |
| def f() -> None: ... |
| |
| x: int |
| |
| class C: |
| def g(self) -> None: ... |
| |
| # Names in __all__ with no definition: |
| # g |
| |
| [case testIgnoreSlots] |
| class A: |
| __slots__ = () |
| [out] |
| class A: ... |
| |
| [case testSkipPrivateProperty] |
| class A: |
| @property |
| def _foo(self): ... |
| [out] |
| class A: ... |
| |
| [case testIncludePrivateProperty] |
| # flags: --include-private |
| class A: |
| @property |
| def _foo(self): ... |
| [out] |
| class A: |
| @property |
| def _foo(self) -> None: ... |
| |
| [case testSkipPrivateStaticAndClassMethod] |
| class A: |
| @staticmethod |
| def _foo(): ... |
| @classmethod |
| def _bar(cls): ... |
| [out] |
| class A: ... |
| |
| [case testIncludePrivateStaticAndClassMethod] |
| # flags: --include-private |
| class A: |
| @staticmethod |
| def _foo(): ... |
| @classmethod |
| def _bar(cls): ... |
| [out] |
| class A: |
| @staticmethod |
| def _foo() -> None: ... |
| @classmethod |
| def _bar(cls) -> None: ... |
| |
| [case testNamedtuple] |
| import collections, typing, x |
| X = collections.namedtuple('X', ['a', 'b']) |
| Y = typing.NamedTuple('Y', [('a', int), ('b', str)]) |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| class Y(NamedTuple): |
| a: int |
| b: str |
| |
| [case testNamedTupleClassSyntax_semanal] |
| from typing import NamedTuple |
| |
| class A(NamedTuple): |
| x: int |
| y: str = 'a' |
| |
| class B(A): |
| z1: str |
| z2 = 1 |
| z3: str = 'b' |
| |
| class RegularClass: |
| x: int |
| y: str = 'a' |
| class NestedNamedTuple(NamedTuple): |
| x: int |
| y: str = 'a' |
| z: str = 'b' |
| [out] |
| from typing import NamedTuple |
| |
| class A(NamedTuple): |
| x: int |
| y: str = ... |
| |
| class B(A): |
| z1: str |
| z2: int |
| z3: str |
| |
| class RegularClass: |
| x: int |
| y: str |
| class NestedNamedTuple(NamedTuple): |
| x: int |
| y: str = ... |
| z: str |
| |
| |
| [case testNestedClassInNamedTuple_semanal-xfail] |
| from typing import NamedTuple |
| |
| # TODO: make sure that nested classes in `NamedTuple` are supported: |
| class NamedTupleWithNestedClass(NamedTuple): |
| class Nested: |
| x: int |
| y: str = 'a' |
| [out] |
| from typing import NamedTuple |
| |
| class NamedTupleWithNestedClass(NamedTuple): |
| class Nested: |
| x: int |
| y: str |
| |
| [case testEmptyNamedtuple] |
| import collections, typing |
| X = collections.namedtuple('X', []) |
| Y = typing.NamedTuple('Y', []) |
| [out] |
| from typing import NamedTuple |
| |
| class X(NamedTuple): ... |
| class Y(NamedTuple): ... |
| |
| [case testNamedtupleAltSyntax] |
| from collections import namedtuple, xx |
| X = namedtuple('X', 'a b') |
| xx |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| [case testNamedtupleAltSyntaxUsingComma] |
| from collections import namedtuple, xx |
| X = namedtuple('X', 'a, b') |
| xx |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| [case testNamedtupleAltSyntaxUsingMultipleCommas] |
| from collections import namedtuple, xx |
| X = namedtuple('X', 'a,, b') |
| xx |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| [case testNamedtupleWithUnderscore] |
| from collections import namedtuple as _namedtuple |
| from typing import NamedTuple as _NamedTuple |
| def f(): ... |
| X = _namedtuple('X', 'a b') |
| Y = _NamedTuple('Y', [('a', int), ('b', str)]) |
| def g(): ... |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| def f() -> None: ... |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| class Y(NamedTuple): |
| a: int |
| b: str |
| |
| def g() -> None: ... |
| |
| [case testNamedtupleBaseClass] |
| import collections, x |
| _X = collections.namedtuple('_X', ['a', 'b']) |
| class Y(_X): ... |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class _X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| class Y(_X): ... |
| |
| [case testNamedtupleAltSyntaxFieldsTuples] |
| from collections import namedtuple, xx |
| from typing import NamedTuple |
| X = namedtuple('X', ()) |
| Y = namedtuple('Y', ('a',)) |
| Z = namedtuple('Z', ('a', 'b', 'c', 'd', 'e')) |
| xx |
| R = NamedTuple('R', ()) |
| S = NamedTuple('S', (('a', int),)) |
| T = NamedTuple('T', (('a', int), ('b', str))) |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): ... |
| |
| class Y(NamedTuple): |
| a: Incomplete |
| |
| class Z(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| c: Incomplete |
| d: Incomplete |
| e: Incomplete |
| |
| class R(NamedTuple): ... |
| |
| class S(NamedTuple): |
| a: int |
| |
| class T(NamedTuple): |
| a: int |
| b: str |
| |
| [case testDynamicNamedTuple] |
| from collections import namedtuple |
| from typing import NamedTuple |
| N = namedtuple('N', ['x', 'y'] + ['z']) |
| M = NamedTuple('M', [('x', int), ('y', str)] + [('z', float)]) |
| class X(namedtuple('X', ['a', 'b'] + ['c'])): ... |
| [out] |
| from _typeshed import Incomplete |
| |
| N: Incomplete |
| M: Incomplete |
| |
| class X(Incomplete): ... |
| |
| [case testNamedTupleInClassBases] |
| import collections, typing |
| from collections import namedtuple |
| from typing import NamedTuple |
| class X(namedtuple('X', ['a', 'b'])): ... |
| class Y(NamedTuple('Y', [('a', int), ('b', str)])): ... |
| class R(collections.namedtuple('R', ['a', 'b'])): ... |
| class S(typing.NamedTuple('S', [('a', int), ('b', str)])): ... |
| [out] |
| import typing |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple('X', [('a', Incomplete), ('b', Incomplete)])): ... |
| class Y(NamedTuple('Y', [('a', int), ('b', str)])): ... |
| class R(NamedTuple('R', [('a', Incomplete), ('b', Incomplete)])): ... |
| class S(typing.NamedTuple('S', [('a', int), ('b', str)])): ... |
| |
| [case testNotNamedTuple] |
| from not_collections import namedtuple |
| from not_typing import NamedTuple |
| from collections import notnamedtuple |
| from typing import NotNamedTuple |
| X = namedtuple('X', ['a', 'b']) |
| Y = notnamedtuple('Y', ['a', 'b']) |
| Z = NamedTuple('Z', [('a', int), ('b', str)]) |
| W = NotNamedTuple('W', [('a', int), ('b', str)]) |
| [out] |
| from _typeshed import Incomplete |
| |
| X: Incomplete |
| Y: Incomplete |
| Z: Incomplete |
| W: Incomplete |
| |
| [case testNamedTupleFromImportAlias] |
| import collections as c |
| import typing as t |
| import typing_extensions as te |
| X = c.namedtuple('X', ['a', 'b']) |
| Y = t.NamedTuple('Y', [('a', int), ('b', str)]) |
| Z = te.NamedTuple('Z', [('a', int), ('b', str)]) |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class X(NamedTuple): |
| a: Incomplete |
| b: Incomplete |
| |
| class Y(NamedTuple): |
| a: int |
| b: str |
| |
| class Z(NamedTuple): |
| a: int |
| b: str |
| |
| [case testArbitraryBaseClass] |
| import x |
| class D(x.C): ... |
| [out] |
| import x |
| |
| class D(x.C): ... |
| |
| [case testArbitraryBaseClass2] |
| import x.y |
| class D(x.y.C): ... |
| [out] |
| import x.y |
| |
| class D(x.y.C): ... |
| |
| [case testUnqualifiedArbitraryBaseClassWithNoDef] |
| class A(int): ... |
| [out] |
| class A(int): ... |
| |
| [case testUnqualifiedArbitraryBaseClass] |
| from x import X |
| class A(X): ... |
| [out] |
| from x import X |
| |
| class A(X): ... |
| |
| [case testUnqualifiedArbitraryBaseClassWithImportAs] |
| from x import X as _X |
| class A(_X): ... |
| [out] |
| from x import X as _X |
| |
| class A(_X): ... |
| |
| [case testGenericClass] |
| class D(Generic[T]): ... |
| [out] |
| class D(Generic[T]): ... |
| |
| [case testGenericClass_semanal] |
| from typing import Generic, TypeVar |
| T = TypeVar('T') |
| class D(Generic[T]): ... |
| [out] |
| from typing import Generic, TypeVar |
| |
| T = TypeVar('T') |
| |
| class D(Generic[T]): ... |
| |
| [case testObjectBaseClass] |
| class A(object): ... |
| [out] |
| class A: ... |
| |
| [case testObjectBaseClassWithImport] |
| import builtins as b |
| class A(b.object): ... |
| [out] |
| class A: ... |
| |
| [case testEmptyLines] |
| def x(): ... |
| def f(): |
| class A: |
| def f(self): |
| self.x = 1 |
| def g(): ... |
| [out] |
| def x() -> None: ... |
| def f() -> None: ... |
| def g() -> None: ... |
| |
| [case testNestedClass] |
| class A: |
| class B: |
| x = 1 |
| def f(self): ... |
| def g(self): ... |
| [out] |
| class A: |
| class B: |
| x: int |
| def f(self) -> None: ... |
| def g(self) -> None: ... |
| |
| [case testExportViaRelativeImport] |
| from .api import get |
| [out] |
| from .api import get as get |
| |
| [case testExportViaRelativePackageImport] |
| from .packages.urllib3.contrib import parse |
| [out] |
| from .packages.urllib3.contrib import parse as parse |
| |
| [case testNoExportViaRelativeImport] |
| from . import get |
| get() |
| [out] |
| |
| [case testRelativeImportAndBase] |
| from .x import X |
| class A(X): |
| pass |
| [out] |
| from .x import X |
| |
| class A(X): ... |
| |
| [case testDuplicateDef] |
| def syslog(a): pass |
| def syslog(a): pass |
| [out] |
| def syslog(a) -> None: ... |
| |
| [case testAsyncAwait_fast_parser] |
| async def f(a): |
| x = await y |
| [out] |
| async def f(a) -> None: ... |
| |
| [case testInferOptionalOnlyFunc] |
| class A: |
| x = None |
| def __init__(self, a=None): |
| self.x = [] |
| def method(self, a=None): |
| self.x = [] |
| [out] |
| from _typeshed import Incomplete |
| |
| class A: |
| x: Incomplete |
| def __init__(self, a: Incomplete | None = ...) -> None: ... |
| def method(self, a: Incomplete | None = ...) -> None: ... |
| |
| [case testAnnotationImportsFrom] |
| import foo |
| from collections import defaultdict |
| x: defaultdict |
| |
| [out] |
| from collections import defaultdict |
| |
| x: defaultdict |
| |
| [case testAnnotationImports] |
| import foo |
| import collections |
| x: collections.defaultdict |
| |
| [out] |
| import collections |
| |
| x: collections.defaultdict |
| |
| |
| [case testAnnotationImports2] |
| from typing import List |
| import collections |
| x: List[collections.defaultdict] |
| |
| [out] |
| import collections |
| from typing import List |
| |
| x: List[collections.defaultdict] |
| |
| |
| [case testAnnotationFwRefs] |
| x: C |
| |
| class C: |
| attr: C |
| |
| y: C |
| [out] |
| x: C |
| |
| class C: |
| attr: C |
| |
| y: C |
| |
| [case testTypeVarPreserved] |
| tv = TypeVar('tv') |
| ps = ParamSpec('ps') |
| tvt = TypeVarTuple('tvt') |
| |
| [out] |
| from typing import TypeVar |
| from typing_extensions import ParamSpec, TypeVarTuple |
| |
| tv = TypeVar('tv') |
| ps = ParamSpec('ps') |
| tvt = TypeVarTuple('tvt') |
| |
| [case testTypeVarArgsPreserved] |
| tv = TypeVar('tv', int, str) |
| |
| [out] |
| from typing import TypeVar |
| |
| tv = TypeVar('tv', int, str) |
| |
| [case testTypeVarNamedArgsPreserved] |
| tv = TypeVar('tv', bound=bool, covariant=True) |
| ps = ParamSpec('ps', bound=bool, covariant=True) |
| |
| [out] |
| from typing import TypeVar |
| from typing_extensions import ParamSpec |
| |
| tv = TypeVar('tv', bound=bool, covariant=True) |
| ps = ParamSpec('ps', bound=bool, covariant=True) |
| |
| [case TypeVarImportAlias] |
| from typing import TypeVar as t_TV, ParamSpec as t_PS |
| from typing_extensions import TypeVar as te_TV, TypeVarTuple as te_TVT |
| from x import TypeVar as x_TV |
| |
| T = t_TV('T') |
| U = te_TV('U') |
| V = x_TV('V') |
| |
| PS = t_PS('PS') |
| TVT = te_TVT('TVT') |
| |
| [out] |
| from _typeshed import Incomplete |
| from typing import ParamSpec as t_PS, TypeVar as t_TV |
| from typing_extensions import TypeVar as te_TV, TypeVarTuple as te_TVT |
| |
| T = t_TV('T') |
| U = te_TV('U') |
| V: Incomplete |
| PS = t_PS('PS') |
| TVT = te_TVT('TVT') |
| |
| [case testTypeVarFromImportAlias] |
| import typing as t |
| import typing_extensions as te |
| import x |
| |
| T = t.TypeVar('T') |
| U = te.TypeVar('U') |
| V = x.TypeVar('V') |
| |
| PS = t.ParamSpec('PS') |
| TVT = te.TypeVarTuple('TVT') |
| |
| [out] |
| import typing as t |
| import typing_extensions as te |
| from _typeshed import Incomplete |
| |
| T = t.TypeVar('T') |
| U = te.TypeVar('U') |
| V: Incomplete |
| PS = t.ParamSpec('PS') |
| TVT = te.TypeVarTuple('TVT') |
| |
| [case testTypeAliasPreserved] |
| alias = str |
| |
| [out] |
| alias = str |
| |
| [case testDeepTypeAliasPreserved] |
| |
| alias = Dict[str, List[str]] |
| |
| [out] |
| alias = Dict[str, List[str]] |
| |
| [case testDeepGenericTypeAliasPreserved] |
| from typing import TypeVar |
| |
| T = TypeVar('T') |
| alias = Union[T, List[T]] |
| |
| [out] |
| from typing import TypeVar |
| |
| T = TypeVar('T') |
| alias = Union[T, List[T]] |
| |
| [case testEllipsisAliasPreserved] |
| |
| alias = Tuple[int, ...] |
| |
| [out] |
| alias = Tuple[int, ...] |
| |
| [case testCallableAliasPreserved] |
| |
| alias1 = Callable[..., int] |
| alias2 = Callable[[str, bool], None] |
| |
| [out] |
| alias1 = Callable[..., int] |
| alias2 = Callable[[str, bool], None] |
| |
| [case testAliasPullsImport] |
| from module import Container |
| |
| alias = Container[Any] |
| |
| [out] |
| from module import Container |
| from typing import Any |
| |
| alias = Container[Any] |
| |
| [case testAliasExceptions] |
| noalias1 = None |
| noalias2 = ... |
| noalias3 = True |
| |
| [out] |
| from _typeshed import Incomplete |
| |
| noalias1: Incomplete |
| noalias2: Incomplete |
| noalias3: bool |
| |
| [case testComplexAlias] |
| # modules: main a |
| |
| from a import valid |
| |
| def func() -> int: |
| return 2 |
| |
| aliased_func = func |
| int_value = 1 |
| |
| class A: |
| cls_var = valid |
| |
| def __init__(self, arg: str) -> None: |
| self.self_var = arg |
| |
| def meth(self) -> None: |
| func_value = int_value |
| |
| alias_meth = meth |
| alias_func = func |
| alias_alias_func = aliased_func |
| int_value = int_value |
| |
| [file a.py] |
| valid : list[int] = [1, 2, 3] |
| |
| |
| [out] |
| # main.pyi |
| from _typeshed import Incomplete |
| from a import valid |
| |
| def func() -> int: ... |
| aliased_func = func |
| int_value: int |
| |
| class A: |
| cls_var = valid |
| self_var: Incomplete |
| def __init__(self, arg: str) -> None: ... |
| def meth(self) -> None: ... |
| alias_meth = meth |
| alias_func = func |
| alias_alias_func = aliased_func |
| int_value = int_value |
| # a.pyi |
| valid: list[int] |
| |
| -- More features/fixes: |
| -- do not export deleted names |
| |
| [case testFunctionNoReturnInfersReturnNone] |
| def f(): |
| x = 1 |
| [out] |
| def f() -> None: ... |
| |
| [case testFunctionReturnNoReturnType] |
| def f(): |
| return 1 |
| def g(): |
| return |
| [out] |
| def f(): ... |
| def g() -> None: ... |
| |
| [case testFunctionEllipsisInfersReturnNone] |
| def f(): ... |
| [out] |
| def f() -> None: ... |
| |
| [case testFunctionYields] |
| def f(): |
| yield 123 |
| def g(): |
| x = yield |
| def h1(): |
| yield |
| return |
| def h2(): |
| yield |
| return "abc" |
| def h3(): |
| yield |
| return None |
| def all(): |
| x = yield 123 |
| return "abc" |
| [out] |
| from _typeshed import Incomplete |
| from collections.abc import Generator |
| |
| def f() -> Generator[Incomplete, None, None]: ... |
| def g() -> Generator[None, Incomplete, None]: ... |
| def h1() -> Generator[None, None, None]: ... |
| def h2() -> Generator[None, None, Incomplete]: ... |
| def h3() -> Generator[None, None, None]: ... |
| def all() -> Generator[Incomplete, Incomplete, Incomplete]: ... |
| |
| [case testFunctionYieldsNone] |
| def f(): |
| yield |
| def g(): |
| yield None |
| |
| [out] |
| from collections.abc import Generator |
| |
| def f() -> Generator[None, None, None]: ... |
| def g() -> Generator[None, None, None]: ... |
| |
| [case testGeneratorAlreadyDefined] |
| class Generator: |
| pass |
| |
| def f(): |
| yield 123 |
| [out] |
| from _typeshed import Incomplete |
| from collections.abc import Generator as _Generator |
| |
| class Generator: ... |
| |
| def f() -> _Generator[Incomplete, None, None]: ... |
| |
| [case testGeneratorYieldFrom] |
| def g1(): |
| yield from x |
| def g2(): |
| y = yield from x |
| def g3(): |
| yield from x |
| return |
| def g4(): |
| yield from x |
| return None |
| def g5(): |
| yield from x |
| return z |
| |
| [out] |
| from _typeshed import Incomplete |
| from collections.abc import Generator |
| |
| def g1() -> Generator[Incomplete, Incomplete, None]: ... |
| def g2() -> Generator[Incomplete, Incomplete, None]: ... |
| def g3() -> Generator[Incomplete, Incomplete, None]: ... |
| def g4() -> Generator[Incomplete, Incomplete, None]: ... |
| def g5() -> Generator[Incomplete, Incomplete, Incomplete]: ... |
| |
| [case testGeneratorYieldAndYieldFrom] |
| def g1(): |
| yield x1 |
| yield from x2 |
| def g2(): |
| yield x1 |
| y = yield from x2 |
| def g3(): |
| y = yield x1 |
| yield from x2 |
| def g4(): |
| yield x1 |
| yield from x2 |
| return |
| def g5(): |
| yield x1 |
| yield from x2 |
| return None |
| def g6(): |
| yield x1 |
| yield from x2 |
| return z |
| def g7(): |
| yield None |
| yield from x2 |
| |
| [out] |
| from _typeshed import Incomplete |
| from collections.abc import Generator |
| |
| def g1() -> Generator[Incomplete, Incomplete, None]: ... |
| def g2() -> Generator[Incomplete, Incomplete, None]: ... |
| def g3() -> Generator[Incomplete, Incomplete, None]: ... |
| def g4() -> Generator[Incomplete, Incomplete, None]: ... |
| def g5() -> Generator[Incomplete, Incomplete, None]: ... |
| def g6() -> Generator[Incomplete, Incomplete, Incomplete]: ... |
| def g7() -> Generator[Incomplete, Incomplete, None]: ... |
| |
| [case testCallable] |
| from typing import Callable |
| |
| x: Callable[[int, int], int] |
| [out] |
| from typing import Callable |
| |
| x: Callable[[int, int], int] |
| |
| [case testAwaitDef] |
| class F: |
| async def f(self): |
| return 1 |
| |
| async def g(): |
| return 2 |
| [out] |
| class F: |
| async def f(self): ... |
| |
| async def g(): ... |
| |
| [case testCoroutineImportAsyncio] |
| import asyncio |
| |
| class F: |
| @asyncio.coroutine |
| def f(self): |
| return 1 |
| |
| @asyncio.coroutine |
| def g(): |
| return 2 |
| |
| @asyncio.coroutine |
| def h(): |
| return 3 |
| [out] |
| import asyncio |
| |
| class F: |
| @asyncio.coroutine |
| def f(self): ... |
| |
| @asyncio.coroutine |
| def g(): ... |
| @asyncio.coroutine |
| def h(): ... |
| |
| [case testCoroutineImportAsyncioCoroutines] |
| import asyncio.coroutines |
| |
| class F: |
| @asyncio.coroutines.coroutine |
| def f(self): |
| return 1 |
| |
| @asyncio.coroutines.coroutine |
| def g(): |
| return 2 |
| [out] |
| import asyncio.coroutines |
| |
| class F: |
| @asyncio.coroutines.coroutine |
| def f(self): ... |
| |
| @asyncio.coroutines.coroutine |
| def g(): ... |
| |
| [case testCoroutineImportAsyncioCoroutinesSub] |
| import asyncio |
| |
| class F: |
| @asyncio.coroutines.coroutine |
| def f(self): |
| return 1 |
| |
| @asyncio.coroutines.coroutine |
| def g(): |
| return 2 |
| [out] |
| import asyncio |
| |
| class F: |
| @asyncio.coroutines.coroutine |
| def f(self): ... |
| |
| @asyncio.coroutines.coroutine |
| def g(): ... |
| |
| [case testCoroutineImportTypes] |
| import types |
| |
| class F: |
| @types.coroutine |
| def f(self): |
| return 1 |
| |
| @types.coroutine |
| def g(): |
| return 2 |
| [out] |
| import types |
| |
| class F: |
| @types.coroutine |
| def f(self): ... |
| |
| @types.coroutine |
| def g(): ... |
| |
| [case testCoroutineFromAsyncioImportCoroutine] |
| from asyncio import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): |
| return 1 |
| |
| @coroutine |
| def g(): |
| return 2 |
| [out] |
| from asyncio import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): ... |
| |
| @coroutine |
| def g(): ... |
| |
| [case testCoroutineFromAsyncioCoroutinesImportCoroutine] |
| from asyncio.coroutines import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): |
| return 1 |
| |
| @coroutine |
| def g(): |
| return 2 |
| [out] |
| from asyncio.coroutines import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): ... |
| |
| @coroutine |
| def g(): ... |
| |
| [case testCoroutineFromTypesImportCoroutine] |
| from types import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): |
| return 1 |
| |
| @coroutine |
| def g(): |
| return 2 |
| [out] |
| from types import coroutine |
| |
| class F: |
| @coroutine |
| def f(self): ... |
| |
| @coroutine |
| def g(): ... |
| |
| [case testCoroutineFromAsyncioImportCoroutineAsC] |
| from asyncio import coroutine as c |
| |
| class F: |
| @c |
| def f(self): |
| return 1 |
| |
| @c |
| def g(): |
| return 2 |
| [out] |
| from asyncio import coroutine as c |
| |
| class F: |
| @c |
| def f(self): ... |
| |
| @c |
| def g(): ... |
| |
| [case testCoroutineFromAsyncioCoroutinesImportCoroutineAsC] |
| from asyncio.coroutines import coroutine as c |
| |
| class F: |
| @c |
| def f(self): |
| return 1 |
| |
| @c |
| def g(): |
| return 2 |
| [out] |
| from asyncio.coroutines import coroutine as c |
| |
| class F: |
| @c |
| def f(self): ... |
| |
| @c |
| def g(): ... |
| |
| [case testCoroutineFromTypesImportCoroutineAsC] |
| from types import coroutine as c |
| |
| class F: |
| @c |
| def f(self): |
| return 1 |
| |
| @c |
| def g(): |
| return 2 |
| [out] |
| from types import coroutine as c |
| |
| class F: |
| @c |
| def f(self): ... |
| |
| @c |
| def g(): ... |
| |
| [case testCoroutineImportAsyncioAsA] |
| import asyncio as a |
| |
| class F: |
| @a.coroutine |
| def f(self): |
| return 1 |
| |
| @a.coroutine |
| def g(): |
| return 2 |
| [out] |
| import asyncio as a |
| |
| class F: |
| @a.coroutine |
| def f(self): ... |
| |
| @a.coroutine |
| def g(): ... |
| |
| [case testCoroutineImportAsyncioCoroutinesAsC] |
| import asyncio.coroutines as c |
| |
| class F: |
| @c.coroutine |
| def f(self): |
| return 1 |
| |
| @c.coroutine |
| def g(): |
| return 2 |
| [out] |
| import asyncio.coroutines as c |
| |
| class F: |
| @c.coroutine |
| def f(self): ... |
| |
| @c.coroutine |
| def g(): ... |
| |
| [case testCoroutineImportAsyncioCoroutinesSubAsA] |
| import asyncio as a |
| |
| class F: |
| @a.coroutines.coroutine |
| def f(self): |
| return 1 |
| |
| @a.coroutines.coroutine |
| def g(): |
| return 2 |
| [out] |
| import asyncio as a |
| |
| class F: |
| @a.coroutines.coroutine |
| def f(self): ... |
| |
| @a.coroutines.coroutine |
| def g(): ... |
| |
| [case testCoroutineImportTypesAsT] |
| import types as t |
| |
| class F: |
| @t.coroutine |
| def f(self): |
| return 1 |
| |
| @t.coroutine |
| def g(): |
| return 2 |
| [out] |
| import types as t |
| |
| class F: |
| @t.coroutine |
| def f(self): ... |
| |
| @t.coroutine |
| def g(): ... |
| |
| |
| -- Tests for stub generation from semantically analyzed trees. |
| -- These tests are much slower, so use the `_semanal` suffix only when needed. |
| |
| [case testNestedClass_semanal] |
| class Outer: |
| class Inner: |
| pass |
| |
| A = Outer.Inner |
| [out] |
| class Outer: |
| class Inner: ... |
| A = Outer.Inner |
| |
| [case testFunctionAlias_semanal] |
| from asyncio import coroutine |
| |
| @coroutine |
| def start_server(): |
| ... |
| |
| start = start_server |
| [out] |
| from asyncio import coroutine |
| |
| @coroutine |
| def start_server() -> None: ... |
| start = start_server |
| |
| [case testModuleAlias_semanal] |
| import a |
| |
| b = a |
| [file a.py] |
| x = 1 |
| [out] |
| import a |
| |
| b = a |
| |
| [case testBadAliasNested_semanal] |
| import a |
| |
| x = registry[a.f] |
| [file a.py] |
| def f(): ... |
| [out] |
| from _typeshed import Incomplete |
| |
| x: Incomplete |
| |
| [case testCrossModuleClass_semanal] |
| import a |
| |
| class C: |
| x: A |
| def f(self) -> A: ... |
| A = a.A |
| [file a.py] |
| class A: ... |
| [out] |
| import a |
| |
| class C: |
| x: A |
| def f(self) -> A: ... |
| A = a.A |
| |
| [case testCrossModuleFunction_semanal] |
| import a |
| g = a.f |
| [file a.py] |
| def f(): ... |
| [out] |
| import a |
| |
| g = a.f |
| |
| [case testPrivateAliasesExcluded_semanal] |
| import a, _a |
| |
| class C: ... |
| |
| A = a._A |
| B = _a.f |
| _C = C |
| [file a.py] |
| class _A: ... |
| [file _a.py] |
| def f(): ... |
| [out] |
| from _typeshed import Incomplete |
| |
| class C: ... |
| |
| A: Incomplete |
| B: Incomplete |
| |
| [case testPrivateAliasesIncluded_semanal] |
| # flags: --include-private |
| import a, _a |
| |
| class C: ... |
| |
| A = a._A |
| B = _a.f |
| _C = C |
| [file a.py] |
| class _A: ... |
| [file _a.py] |
| def f(): ... |
| [out] |
| import _a |
| import a |
| |
| class C: ... |
| A = a._A |
| B = _a.f |
| _C = C |
| |
| [case testFinalWrapped_semanal] |
| from typing import Final |
| |
| x: Final = 1 |
| y: Final = x |
| z: Final[object] |
| t: Final |
| [out] |
| from _typeshed import Incomplete |
| from typing import Final |
| |
| x: Final[int] |
| y: Final[Incomplete] |
| z: Final[object] |
| t: Final[Incomplete] |
| |
| [case testFinalInvalid_semanal] |
| Final = 'boom' |
| |
| x: Final = 1 |
| [out] |
| Final: str |
| x: Final |
| |
| [case testNoFunctionNested_semanal] |
| import a |
| from typing import Dict, Any |
| |
| funcs: Dict[Any, Any] |
| f = funcs[a.f] |
| [out] |
| from _typeshed import Incomplete |
| from typing import Any, Dict |
| |
| funcs: Dict[Any, Any] |
| f: Incomplete |
| |
| [case testAbstractMethodNameExpr] |
| from abc import ABCMeta, abstractmethod |
| |
| class A(metaclass=ABCMeta): |
| @abstractmethod |
| def meth(self): |
| pass |
| [out] |
| from abc import ABCMeta, abstractmethod |
| |
| class A(metaclass=ABCMeta): |
| @abstractmethod |
| def meth(self): ... |
| |
| [case testAbstractMethodMemberExpr] |
| import abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @abc.abstractmethod |
| def meth(self): |
| pass |
| [out] |
| import abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @abc.abstractmethod |
| def meth(self): ... |
| |
| [case testAbstractMethodMemberExpr2] |
| import abc as _abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @_abc.abstractmethod |
| def meth(self): |
| pass |
| [out] |
| import abc as _abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @_abc.abstractmethod |
| def meth(self): ... |
| |
| [case testABCMeta_semanal] |
| from base import Base |
| from abc import abstractmethod |
| |
| class C(Base): |
| @abstractmethod |
| def other(self): |
| pass |
| |
| [file base.py] |
| from abc import abstractmethod, ABCMeta |
| |
| class Base(metaclass=ABCMeta): |
| @abstractmethod |
| def meth(self): |
| pass |
| [out] |
| import abc |
| from abc import abstractmethod |
| from base import Base |
| |
| class C(Base, metaclass=abc.ABCMeta): |
| @abstractmethod |
| def other(self): ... |
| |
| [case testInvalidNumberOfArgsInAnnotation] |
| def f(x): |
| # type: () -> int |
| return '' |
| |
| [out] |
| def f(x): ... |
| |
| [case testFunctionPartiallyAnnotated] |
| def f(x) -> None: |
| pass |
| |
| def g(x, y: str): |
| pass |
| |
| class A: |
| def f(self, x) -> None: |
| pass |
| |
| [out] |
| def f(x) -> None: ... |
| def g(x, y: str): ... |
| |
| class A: |
| def f(self, x) -> None: ... |
| |
| [case testExplicitAnyArg] |
| from typing import Any |
| |
| def f(x: Any): |
| pass |
| def g(x, y: Any) -> str: |
| pass |
| def h(x: Any) -> str: |
| pass |
| |
| [out] |
| from typing import Any |
| |
| def f(x: Any): ... |
| def g(x, y: Any) -> str: ... |
| def h(x: Any) -> str: ... |
| |
| [case testExplicitReturnedAny] |
| from typing import Any |
| |
| def f(x: str) -> Any: |
| pass |
| def g(x, y: str) -> Any: |
| pass |
| def h(x) -> Any: |
| pass |
| |
| [out] |
| from typing import Any |
| |
| def f(x: str) -> Any: ... |
| def g(x, y: str) -> Any: ... |
| def h(x) -> Any: ... |
| |
| [case testPlacementOfDecorators] |
| class A: |
| @property |
| def x(self): |
| self.y = 'y' |
| return 'x' |
| |
| class B: |
| @property |
| def x(self): |
| return 'x' |
| |
| @x.setter |
| def x(self, value): |
| self.y = 'y' |
| |
| [out] |
| class A: |
| y: str |
| @property |
| def x(self): ... |
| |
| class B: |
| @property |
| def x(self): ... |
| y: str |
| @x.setter |
| def x(self, value) -> None: ... |
| |
| [case testMisplacedTypeComment] |
| def f(): |
| x = 0 |
| |
| # type: str |
| y = '' |
| |
| [out] |
| def f() -> None: ... |
| |
| [case testConditionalImportAll_semanal] |
| __all__ = ['cookielib'] |
| |
| if object(): |
| from http import cookiejar as cookielib |
| else: |
| import cookielib |
| |
| [out] |
| import cookielib as cookielib |
| |
| [case testCannotCalculateMRO_semanal] |
| class X: pass |
| |
| class int(int, X): # Cycle |
| pass |
| |
| class A: pass |
| class B(A): pass |
| class C(B): pass |
| class D(A, B): pass # No consistent method resolution order |
| class E(C, D): pass # Ditto |
| |
| [out] |
| class X: ... |
| class int(int, X): ... |
| class A: ... |
| class B(A): ... |
| class C(B): ... |
| class D(A, B): ... |
| class E(C, D): ... |
| |
| [case testUnreachableCode_semanal] |
| MYPY = False |
| class A: pass |
| if MYPY: |
| class C(A): |
| def f(self) -> None: pass |
| else: |
| def f(i): |
| return i |
| |
| class C(A): |
| def g(self) -> None: pass |
| [out] |
| MYPY: bool |
| |
| class A: ... |
| |
| class C(A): |
| def f(self) -> None: ... |
| |
| [case testAbstractPropertyImportAlias] |
| import abc as abc_alias |
| |
| class A: |
| @abc_alias.abstractproperty |
| def x(self): pass |
| |
| [out] |
| import abc as abc_alias |
| |
| class A: |
| @property |
| @abc_alias.abstractmethod |
| def x(self): ... |
| |
| [case testAbstractPropertyFromImportAlias] |
| from abc import abstractproperty as ap |
| |
| class A: |
| @ap |
| def x(self): pass |
| |
| [out] |
| import abc |
| |
| class A: |
| @property |
| @abc.abstractmethod |
| def x(self): ... |
| |
| [case testAbstractProperty1_semanal] |
| import other |
| import abc |
| |
| class A: |
| @abc.abstractproperty |
| def x(self): pass |
| |
| [out] |
| import abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @property |
| @abc.abstractmethod |
| def x(self): ... |
| |
| [case testAbstractProperty2_semanal] |
| import other |
| from abc import abstractproperty |
| |
| class A: |
| @abstractproperty |
| def x(self): pass |
| |
| [out] |
| import abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @property |
| @abc.abstractmethod |
| def x(self): ... |
| |
| [case testAbstractProperty3_semanal] |
| import other |
| from abc import abstractproperty as alias_name |
| |
| class A: |
| @alias_name |
| def x(self): pass |
| |
| [out] |
| import abc |
| |
| class A(metaclass=abc.ABCMeta): |
| @property |
| @abc.abstractmethod |
| def x(self): ... |
| |
| [case testClassWithNameIncompleteOrOptional] |
| Y = object() |
| |
| def g(x=None): pass |
| |
| x = g() |
| |
| class Incomplete: |
| pass |
| |
| def Optional(): |
| return 0 |
| |
| [out] |
| from _typeshed import Incomplete as _Incomplete |
| |
| Y: _Incomplete |
| |
| def g(x: _Incomplete | None = ...) -> None: ... |
| |
| x: _Incomplete |
| |
| class Incomplete: ... |
| |
| def Optional(): ... |
| |
| [case testExportedNameImported] |
| # modules: main a b |
| from a import C |
| |
| class D(C): pass |
| |
| [file a.py] |
| from b import C |
| |
| [file b.py] |
| class C: pass |
| |
| [out] |
| # main.pyi |
| from a import C |
| |
| class D(C): ... |
| # a.pyi |
| from b import C as C |
| # b.pyi |
| class C: ... |
| |
| [case testVendoredSix] |
| from p1.vendored import six |
| from p1.vendor.six import foobar |
| from p1.packages.six.moves import http_client |
| from .packages.six.moves import queue |
| from p1.vendored.six.moves.http_client import foo |
| from p1.vendored.six.moves.urllib.parse import bar |
| |
| class C(http_client.HTTPMessage): pass |
| class D(six.Iterator): pass |
| |
| [out] |
| import six |
| from six import foobar as foobar |
| from six.moves import http_client, queue as queue |
| from six.moves.http_client import foo as foo |
| from six.moves.urllib.parse import bar as bar |
| |
| class C(http_client.HTTPMessage): ... |
| class D(six.Iterator): ... |
| |
| [case testVendoredPackage] |
| # modules: main p.vendored.requests p.sub.requests |
| from p.vendored.requests import Request |
| from p.sub.requests import Request2 |
| |
| x = Request() |
| y = Request2() |
| |
| [file p/__init__.py] |
| |
| [file p/vendored/__init__.py] |
| |
| [file p/vendored/requests.py] |
| class Request: |
| pass |
| |
| [file p/sub/__init__.py] |
| |
| [file p/sub/requests.py] |
| class Request2: |
| pass |
| |
| [out] |
| # main.pyi |
| from _typeshed import Incomplete |
| |
| x: Incomplete |
| y: Incomplete |
| <out/p/vendored/requests.pyi was not generated> |
| # p/sub/requests.pyi |
| class Request2: ... |
| |
| [case testTestFiles] |
| # modules: p p.x p.tests p.tests.test_foo |
| |
| [file p/__init__.py] |
| def f(): pass |
| |
| [file p/x.py] |
| def g(): pass |
| |
| [file p/tests/__init__.py] |
| |
| [file p/tests/test_foo.py] |
| def test_thing(): pass |
| |
| [out] |
| # p/__init__.pyi |
| def f() -> None: ... |
| # p/x.pyi |
| def g() -> None: ... |
| <out/p/tests.pyi was not generated> |
| <out/p/tests/test_foo.pyi was not generated> |
| |
| [case testTestFiles_import] |
| # modules: p p.x p.tests p.tests.test_foo |
| |
| [file p/__init__.py] |
| def f(): pass |
| |
| [file p/x.py] |
| def g(): pass |
| |
| [file p/tests/__init__.py] |
| |
| [file p/tests/test_foo.py] |
| def test_thing(): pass |
| |
| [out] |
| # p/__init__.pyi |
| def f() -> None: ... |
| # p/x.pyi |
| def g() -> None: ... |
| <out/p/tests.pyi was not generated> |
| <out/p/tests/test_foo.pyi was not generated> |
| |
| [case testVerboseFlag] |
| # Just test that --verbose does not break anything in a basic test case. |
| # flags: --verbose |
| |
| def f(x, y): pass |
| [out] |
| def f(x, y) -> None: ... |
| |
| [case testImportedModuleExits_import] |
| # modules: a b c |
| |
| [file a.py] |
| def g(): pass |
| |
| [file b.py] |
| import sys |
| def f(): pass |
| sys.exit(1) |
| |
| [file c.py] |
| x = 0 |
| |
| [out] |
| # a.pyi |
| def g() -> None: ... |
| # b.pyi |
| def f() -> None: ... |
| # c.pyi |
| x: int |
| |
| [case testImportedModuleHardExits_import] |
| # modules: a b c |
| |
| [file a.py] |
| def g(): pass |
| |
| [file b.py] |
| import os |
| def f(): pass |
| os._exit(1) # Kill process |
| |
| [file c.py] |
| x = 0 |
| |
| [out] |
| # a.pyi |
| def g() -> None: ... |
| # b.pyi |
| def f() -> None: ... |
| # c.pyi |
| x: int |
| |
| [case testImportedModuleHardExits2_import] |
| # modules: p/a p/b p/c |
| |
| [file p/__init__.py] |
| |
| [file p/a.py] |
| def g(): pass |
| |
| [file p/b.py] |
| import os |
| def f(): pass |
| os._exit(1) # Kill process |
| |
| [file p/c.py] |
| x = 0 |
| |
| [out] |
| # p/a.pyi |
| def g() -> None: ... |
| # p/b.pyi |
| def f() -> None: ... |
| # p/c.pyi |
| x: int |
| |
| [case testImportedModuleHardExits3_import] |
| # modules: p p/a |
| |
| [file p/__init__.py] |
| import os |
| def f(): pass |
| os._exit(1) # Kill process |
| |
| [file p/a.py] |
| def g(): pass |
| |
| [out] |
| # p/__init__.pyi |
| def f() -> None: ... |
| # p/a.pyi |
| def g() -> None: ... |
| |
| [case testImportedModuleHardExits4_import] |
| # flags: -p p |
| # modules: p p/a |
| |
| [file p/__init__.py] |
| def ff(): pass |
| |
| [file p/a.py] |
| import os |
| def gg(): pass |
| os._exit(1) # Kill process |
| |
| [out] |
| # p/__init__.pyi |
| def ff() -> None: ... |
| # p/a.pyi |
| def gg() -> None: ... |
| |
| [case testExportInternalImportsByDefault] |
| # modules: p p/a |
| |
| [file p/__init__.py] |
| from p.a import A, f |
| from m import C |
| |
| a: A |
| c: C |
| f() |
| |
| [file p/a.py] |
| class A: pass |
| def f(): pass |
| |
| [file m.py] |
| class C: pass |
| |
| [out] |
| # p/__init__.pyi |
| from m import C |
| from p.a import A as A, f as f |
| |
| a: A |
| c: C |
| # p/a.pyi |
| class A: ... |
| |
| def f() -> None: ... |
| |
| [case testNoExportOfInternalImportsIfAll_import] |
| # modules: p p/a |
| |
| [file p/__init__.py] |
| from p.a import A |
| |
| __all__ = ['a'] |
| |
| a = None # type: A |
| b = 0 # type: int |
| |
| [file p/a.py] |
| class A: pass |
| |
| [out] |
| # p/__init__.pyi |
| from p.a import A |
| |
| a: A |
| # p/a.pyi |
| class A: ... |
| |
| [case testExportInternalImportsByDefaultFromUnderscorePackage] |
| # modules: p |
| |
| [file p.py] |
| from _p import A |
| from _m import B |
| from _pm import C |
| |
| a: A |
| b: B |
| c: C |
| |
| [file _p.py] |
| class A: pass |
| |
| [file _m.py] |
| class B: pass |
| |
| [file _pm.py] |
| class C: pass |
| |
| [out] |
| from _m import B |
| from _p import A as A |
| from _pm import C |
| |
| a: A |
| b: B |
| c: C |
| |
| [case testDisableExportOfInternalImports] |
| # flags: --export-less |
| # modules: p p/a |
| |
| [file p/__init__.py] |
| from p.a import A, B |
| from m import C |
| |
| a: A |
| c: C |
| |
| [file p/a.py] |
| class A: pass |
| class B: pass |
| |
| [file m.py] |
| class C: pass |
| |
| [out] |
| # p/__init__.pyi |
| from m import C |
| from p.a import A, B as B |
| |
| a: A |
| c: C |
| # p/a.pyi |
| class A: ... |
| class B: ... |
| |
| [case testExportInternalImportsByDefaultUsingRelativeImport] |
| # modules: p.a |
| |
| [file p/__init__.py] |
| |
| [file p/a.py] |
| from .b import f |
| f() |
| |
| [file p/b.py] |
| def f(): pass |
| |
| [out] |
| from .b import f as f |
| |
| [case testExportInternalImportsByDefaultSkipPrivate] |
| # modules: p.a |
| |
| [file p/__init__.py] |
| |
| [file p/a.py] |
| from .b import _f, _g as _g, _i |
| from p.b import _h |
| _f() |
| _h() |
| |
| [file p/b.py] |
| def _f(): pass |
| def _g(): pass |
| def _h(): pass |
| def _i(): pass |
| x = 0 |
| |
| [out] |
| |
| [case testExportInternalImportsByDefaultIncludePrivate] |
| # flags: --include-private |
| # modules: p.a |
| |
| [file p/__init__.py] |
| |
| [file p/a.py] |
| from .b import _f |
| _f() |
| |
| [file p/b.py] |
| def _f(): pass |
| |
| [out] |
| from .b import _f as _f |
| |
| [case testHideDunderModuleAttributes] |
| from m import ( |
| __about__, |
| __author__, |
| __copyright__, |
| __email__, |
| __license__, |
| __summary__, |
| __title__, |
| __uri__, |
| __version__ |
| ) |
| |
| class A: |
| __uri__ = 0 |
| |
| [file m.py] |
| __about__ = '' |
| __author__ = '' |
| __copyright__ = '' |
| __email__ = '' |
| __license__ = '' |
| __summary__ = '' |
| __title__ = '' |
| __uri__ = '' |
| __version__ = '' |
| |
| [out] |
| class A: ... |
| |
| [case testHideDunderModuleAttributesWithAll_import] |
| from m import ( |
| __about__, |
| __author__, |
| __copyright__, |
| __email__, |
| __license__, |
| __summary__, |
| __title__, |
| __uri__, |
| __version__ |
| ) |
| |
| __all__ = ['__about__', '__author__', '__version__'] |
| |
| [file m.py] |
| __about__ = '' |
| __author__ = '' |
| __copyright__ = '' |
| __email__ = '' |
| __license__ = '' |
| __summary__ = '' |
| __title__ = '' |
| __uri__ = '' |
| __version__ = '' |
| |
| [out] |
| |
| [case testAttrsClass_semanal] |
| import attrs |
| |
| @attrs.define |
| class C: |
| x = attrs.field() |
| |
| [out] |
| from _typeshed import Incomplete |
| |
| class C: |
| x: Incomplete |
| def __init__(self, x) -> None: ... |
| def __lt__(self, other): ... |
| def __le__(self, other): ... |
| def __gt__(self, other): ... |
| def __ge__(self, other): ... |
| |
| [case testNamedTupleInClass] |
| from collections import namedtuple |
| |
| class C: |
| N = namedtuple('N', ['x', 'y']) |
| [out] |
| from _typeshed import Incomplete |
| from typing import NamedTuple |
| |
| class C: |
| class N(NamedTuple): |
| x: Incomplete |
| y: Incomplete |
| |
| [case testImports_directImportsWithAlias] |
| import p.a as a |
| import p.b as b |
| |
| x: a.X |
| y: b.Y |
| |
| [out] |
| import p.a as a |
| import p.b as b |
| |
| x: a.X |
| y: b.Y |
| |
| [case testImports_directImportsMixed] |
| import p.a |
| import p.a as a |
| import p.b as b |
| |
| x: a.X |
| y: b.Y |
| z: p.a.X |
| |
| [out] |
| import p.a as a |
| import p.b as b |
| import p.a |
| |
| x: a.X |
| y: b.Y |
| z: p.a.X |
| |
| [case testImport_overwrites_directWithAlias_from] |
| import p.a as a |
| from p import a |
| |
| x: a.X |
| |
| [out] |
| from p import a as a |
| |
| x: a.X |
| |
| [case testImport_overwrites_directWithAlias_fromWithAlias] |
| import p.a as a |
| from p import b as a |
| |
| x: a.X |
| |
| [out] |
| from p import b as a |
| |
| x: a.X |
| |
| [case testImports_overwrites_direct_from] |
| import a |
| from p import a |
| |
| x: a.X |
| |
| [out] |
| from p import a as a |
| |
| x: a.X |
| |
| [case testImports_overwrites_direct_fromWithAlias] |
| import a |
| from p import b as a |
| |
| x: a.X |
| |
| [out] |
| from p import b as a |
| |
| x: a.X |
| |
| [case testImports_overwrites_from_directWithAlias] |
| from p import a |
| import p.a as a |
| |
| x: a.X |
| |
| [out] |
| import p.a as a |
| |
| x: a.X |
| |
| [case testImports_overwrites_fromWithAlias_direct] |
| import a |
| from p import b as a |
| |
| x: a.X |
| |
| [out] |
| from p import b as a |
| |
| x: a.X |
| |
| [case testImports_direct] |
| import p.a |
| import pp |
| |
| x: a.X |
| y: p.a.Y |
| |
| [out] |
| import p.a |
| |
| x: a.X |
| y: p.a.Y |
| |
| [case testOverload_fromTypingImport] |
| from typing import Tuple, Union, overload |
| |
| class A: |
| @overload |
| def f(self, x: int, y: int) -> int: |
| ... |
| |
| @overload |
| def f(self, x: Tuple[int, int]) -> int: |
| ... |
| |
| def f(self, *args: Union[int, Tuple[int, int]]) -> int: |
| pass |
| |
| @overload |
| def f(x: int, y: int) -> int: |
| ... |
| |
| @overload |
| def f(x: Tuple[int, int]) -> int: |
| ... |
| |
| def f(*args: Union[int, Tuple[int, int]]) -> int: |
| pass |
| |
| |
| [out] |
| from typing import Tuple, overload |
| |
| class A: |
| @overload |
| def f(self, x: int, y: int) -> int: ... |
| @overload |
| def f(self, x: Tuple[int, int]) -> int: ... |
| |
| |
| @overload |
| def f(x: int, y: int) -> int: ... |
| @overload |
| def f(x: Tuple[int, int]) -> int: ... |
| |
| [case testOverload_fromTypingExtensionsImport] |
| from typing import Tuple, Union |
| from typing_extensions import overload |
| |
| class A: |
| @overload |
| def f(self, x: int, y: int) -> int: |
| ... |
| |
| @overload |
| def f(self, x: Tuple[int, int]) -> int: |
| ... |
| |
| def f(self, *args: Union[int, Tuple[int, int]]) -> int: |
| pass |
| |
| @overload |
| def f(x: int, y: int) -> int: |
| ... |
| |
| @overload |
| def f(x: Tuple[int, int]) -> int: |
| ... |
| |
| def f(*args: Union[int, Tuple[int, int]]) -> int: |
| pass |
| |
| |
| [out] |
| from typing import Tuple |
| from typing_extensions import overload |
| |
| class A: |
| @overload |
| def f(self, x: int, y: int) -> int: ... |
| @overload |
| def f(self, x: Tuple[int, int]) -> int: ... |
| |
| |
| @overload |
| def f(x: int, y: int) -> int: ... |
| @overload |
| def f(x: Tuple[int, int]) -> int: ... |
| |
| [case testOverload_importTyping] |
| import typing |
| import typing_extensions |
| |
| class A: |
| @typing.overload |
| def f(self, x: int, y: int) -> int: |
| ... |
| |
| @typing.overload |
| def f(self, x: typing.Tuple[int, int]) -> int: |
| ... |
| |
| def f(self, *args: typing.Union[int, typing.Tuple[int, int]]) -> int: |
| pass |
| |
| @typing.overload |
| @classmethod |
| def g(cls, x: int, y: int) -> int: |
| ... |
| |
| @typing.overload |
| @classmethod |
| def g(cls, x: typing.Tuple[int, int]) -> int: |
| ... |
| |
| @classmethod |
| def g(self, *args: typing.Union[int, typing.Tuple[int, int]]) -> int: |
| pass |
| |
| @typing.overload |
| def f(x: int, y: int) -> int: |
| ... |
| |
| @typing.overload |
| def f(x: typing.Tuple[int, int]) -> int: |
| ... |
| |
| def f(*args: typing.Union[int, typing.Tuple[int, int]]) -> int: |
| pass |
| |
| @typing_extensions.overload |
| def g(x: int, y: int) -> int: |
| ... |
| |
| @typing_extensions.overload |
| def g(x: typing.Tuple[int, int]) -> int: |
| ... |
| |
| def g(*args: typing.Union[int, typing.Tuple[int, int]]) -> int: |
| pass |
| |
| |
| [out] |
| import typing |
| import typing_extensions |
| |
| class A: |
| @typing.overload |
| def f(self, x: int, y: int) -> int: ... |
| @typing.overload |
| def f(self, x: typing.Tuple[int, int]) -> int: ... |
| @typing.overload |
| @classmethod |
| def g(cls, x: int, y: int) -> int: ... |
| @typing.overload |
| @classmethod |
| def g(cls, x: typing.Tuple[int, int]) -> int: ... |
| |
| |
| @typing.overload |
| def f(x: int, y: int) -> int: ... |
| @typing.overload |
| def f(x: typing.Tuple[int, int]) -> int: ... |
| @typing_extensions.overload |
| def g(x: int, y: int) -> int: ... |
| @typing_extensions.overload |
| def g(x: typing.Tuple[int, int]) -> int: ... |
| |
| [case testOverload_importTypingAs] |
| import typing as t |
| import typing_extensions as te |
| |
| class A: |
| @t.overload |
| def f(self, x: int, y: int) -> int: |
| ... |
| |
| @t.overload |
| def f(self, x: t.Tuple[int, int]) -> int: |
| ... |
| |
| def f(self, *args: typing.Union[int, t.Tuple[int, int]]) -> int: |
| pass |
| |
| @t.overload |
| @classmethod |
| def g(cls, x: int, y: int) -> int: |
| ... |
| |
| @t.overload |
| @classmethod |
| def g(cls, x: t.Tuple[int, int]) -> int: |
| ... |
| |
| @classmethod |
| def g(self, *args: t.Union[int, t.Tuple[int, int]]) -> int: |
| pass |
| |
| @t.overload |
| def f(x: int, y: int) -> int: |
| ... |
| |
| @t.overload |
| def f(x: t.Tuple[int, int]) -> int: |
| ... |
| |
| def f(*args: t.Union[int, t.Tuple[int, int]]) -> int: |
| pass |
| |
| |
| @te.overload |
| def g(x: int, y: int) -> int: |
| ... |
| |
| @te.overload |
| def g(x: t.Tuple[int, int]) -> int: |
| ... |
| |
| def g(*args: t.Union[int, t.Tuple[int, int]]) -> int: |
| pass |
| |
| [out] |
| import typing as t |
| import typing_extensions as te |
| |
| class A: |
| @t.overload |
| def f(self, x: int, y: int) -> int: ... |
| @t.overload |
| def f(self, x: t.Tuple[int, int]) -> int: ... |
| @t.overload |
| @classmethod |
| def g(cls, x: int, y: int) -> int: ... |
| @t.overload |
| @classmethod |
| def g(cls, x: t.Tuple[int, int]) -> int: ... |
| |
| |
| @t.overload |
| def f(x: int, y: int) -> int: ... |
| @t.overload |
| def f(x: t.Tuple[int, int]) -> int: ... |
| @te.overload |
| def g(x: int, y: int) -> int: ... |
| @te.overload |
| def g(x: t.Tuple[int, int]) -> int: ... |
| |
| [case testOverloadFromImportAlias] |
| from typing import overload as t_overload |
| from typing_extensions import overload as te_overload |
| |
| @t_overload |
| def f(x: int, y: int) -> int: |
| ... |
| |
| @te_overload |
| def g(x: int, y: int) -> int: |
| ... |
| |
| [out] |
| from typing import overload as t_overload |
| from typing_extensions import overload as te_overload |
| |
| @t_overload |
| def f(x: int, y: int) -> int: ... |
| @te_overload |
| def g(x: int, y: int) -> int: ... |
| |
| [case testProtocol_semanal] |
| from typing import Protocol, TypeVar |
| |
| class P(Protocol): |
| def f(self, x: int, y: int) -> str: |
| ... |
| |
| T = TypeVar('T') |
| T2 = TypeVar('T2') |
| class PT(Protocol[T, T2]): |
| def f(self, x: T) -> T2: |
| ... |
| |
| [out] |
| from typing import Protocol, TypeVar |
| |
| class P(Protocol): |
| def f(self, x: int, y: int) -> str: ... |
| T = TypeVar('T') |
| T2 = TypeVar('T2') |
| |
| class PT(Protocol[T, T2]): |
| def f(self, x: T) -> T2: ... |
| |
| [case testProtocolAbstractMethod_semanal] |
| from abc import abstractmethod |
| from typing import Protocol |
| |
| class P(Protocol): |
| @abstractmethod |
| def f(self, x: int, y: int) -> str: |
| ... |
| |
| [out] |
| from abc import abstractmethod |
| from typing import Protocol |
| |
| class P(Protocol): |
| @abstractmethod |
| def f(self, x: int, y: int) -> str: ... |
| |
| [case testNonDefaultKeywordOnlyArgAfterAsterisk] |
| def func(*, non_default_kwarg: bool, default_kwarg: bool = True): ... |
| [out] |
| def func(*, non_default_kwarg: bool, default_kwarg: bool = ...): ... |
| |
| [case testNestedGenerator] |
| def f1(): |
| def g(): |
| yield 0 |
| return 0 |
| def f2(): |
| def g(): |
| yield from [0] |
| return 0 |
| [out] |
| def f1(): ... |
| def f2(): ... |
| |
| [case testIncludeDocstrings] |
| # flags: --include-docstrings |
| class A: |
| """class docstring |
| |
| a multiline docstring""" |
| def func(): |
| """func docstring |
| don't forget to indent""" |
| ... |
| def nodoc(): |
| ... |
| class B: |
| def quoteA(): |
| '''func docstring with quotes"""\\n |
| and an end quote\'''' |
| ... |
| def quoteB(): |
| '''func docstring with quotes""" |
| \'\'\' |
| and an end quote\\"''' |
| ... |
| def quoteC(): |
| """func docstring with end quote\\\"""" |
| ... |
| def quoteD(): |
| r'''raw with quotes\"''' |
| ... |
| [out] |
| class A: |
| """class docstring |
| |
| a multiline docstring""" |
| def func() -> None: |
| """func docstring |
| don't forget to indent""" |
| def nodoc() -> None: ... |
| |
| class B: |
| def quoteA() -> None: |
| '''func docstring with quotes"""\\n |
| and an end quote\'''' |
| def quoteB() -> None: |
| '''func docstring with quotes""" |
| \'\'\' |
| and an end quote\\"''' |
| def quoteC() -> None: |
| '''func docstring with end quote\\"''' |
| def quoteD() -> None: |
| '''raw with quotes\\"''' |
| |
| [case testIgnoreDocstrings] |
| class A: |
| """class docstring |
| |
| a multiline docstring""" |
| def func(): |
| """func docstring |
| |
| don't forget to indent""" |
| def nodoc(): |
| ... |
| |
| class B: |
| def func(): |
| """func docstring""" |
| ... |
| def nodoc(): |
| ... |
| |
| [out] |
| class A: |
| def func() -> None: ... |
| def nodoc() -> None: ... |
| |
| class B: |
| def func() -> None: ... |
| def nodoc() -> None: ... |
| |
| [case testKnownMagicMethodsReturnTypes] |
| class Some: |
| def __len__(self): ... |
| def __length_hint__(self): ... |
| def __init__(self): ... |
| def __del__(self): ... |
| def __bool__(self): ... |
| def __bytes__(self): ... |
| def __format__(self, spec): ... |
| def __contains__(self, obj): ... |
| def __complex__(self): ... |
| def __int__(self): ... |
| def __float__(self): ... |
| def __index__(self): ... |
| [out] |
| class Some: |
| def __len__(self) -> int: ... |
| def __length_hint__(self) -> int: ... |
| def __init__(self) -> None: ... |
| def __del__(self) -> None: ... |
| def __bool__(self) -> bool: ... |
| def __bytes__(self) -> bytes: ... |
| def __format__(self, spec) -> str: ... |
| def __contains__(self, obj) -> bool: ... |
| def __complex__(self) -> complex: ... |
| def __int__(self) -> int: ... |
| def __float__(self) -> float: ... |
| def __index__(self) -> int: ... |
| |
| |
| [case testTypeVarPEP604Bound] |
| from typing import TypeVar |
| T = TypeVar("T", bound=str | None) |
| [out] |
| from typing import TypeVar |
| |
| T = TypeVar('T', bound=str | None) |
| |
| |
| [case testPEP604UnionType] |
| a: str | int |
| |
| def f(x: str | None) -> None: ... |
| [out] |
| a: str | int |
| |
| def f(x: str | None) -> None: ... |
| |
| [case testTypeddict] |
| import typing, x |
| X = typing.TypedDict('X', {'a': int, 'b': str}) |
| Y = typing.TypedDict('X', {'a': int, 'b': str}, total=False) |
| [out] |
| from typing_extensions import TypedDict |
| |
| class X(TypedDict): |
| a: int |
| b: str |
| |
| class Y(TypedDict, total=False): |
| a: int |
| b: str |
| |
| [case testTypeddictClassWithKeyword] |
| from typing import TypedDict |
| class MyDict(TypedDict, total=False): |
| foo: str |
| bar: int |
| [out] |
| from typing import TypedDict |
| |
| class MyDict(TypedDict, total=False): |
| foo: str |
| bar: int |
| |
| [case testTypeddictKeywordSyntax] |
| from typing import TypedDict |
| |
| X = TypedDict('X', a=int, b=str) |
| Y = TypedDict('X', a=int, b=str, total=False) |
| [out] |
| from typing import TypedDict |
| |
| class X(TypedDict): |
| a: int |
| b: str |
| |
| class Y(TypedDict, total=False): |
| a: int |
| b: str |
| |
| [case testTypeddictWithNonIdentifierOrKeywordKeys] |
| from typing import TypedDict |
| X = TypedDict('X', {'a-b': int, 'c': str}) |
| Y = TypedDict('X', {'a-b': int, 'c': str}, total=False) |
| Z = TypedDict('X', {'a': int, 'in': str}) |
| [out] |
| from typing import TypedDict |
| |
| X = TypedDict('X', {'a-b': int, 'c': str}) |
| Y = TypedDict('X', {'a-b': int, 'c': str}, total=False) |
| Z = TypedDict('X', {'a': int, 'in': str}) |
| |
| [case testEmptyTypeddict] |
| import typing |
| X = typing.TypedDict('X', {}) |
| Y = typing.TypedDict('Y', {}, total=False) |
| Z = typing.TypedDict('Z') |
| W = typing.TypedDict('W', total=False) |
| [out] |
| from typing_extensions import TypedDict |
| |
| class X(TypedDict): ... |
| class Y(TypedDict, total=False): ... |
| class Z(TypedDict): ... |
| class W(TypedDict, total=False): ... |
| |
| [case testTypeddictAliased] |
| from typing import TypedDict as t_TypedDict |
| from typing_extensions import TypedDict as te_TypedDict |
| def f(): ... |
| X = t_TypedDict('X', {'a': int, 'b': str}) |
| Y = te_TypedDict('Y', {'a': int, 'b': str}) |
| def g(): ... |
| [out] |
| from typing_extensions import TypedDict |
| |
| def f() -> None: ... |
| |
| class X(TypedDict): |
| a: int |
| b: str |
| |
| class Y(TypedDict): |
| a: int |
| b: str |
| |
| def g() -> None: ... |
| |
| [case testTypeddictFromImportAlias] |
| import typing as t |
| import typing_extensions as te |
| X = t.TypedDict('X', {'a': int, 'b': str}) |
| Y = te.TypedDict('Y', {'a': int, 'b': str}) |
| [out] |
| from typing_extensions import TypedDict |
| |
| class X(TypedDict): |
| a: int |
| b: str |
| |
| class Y(TypedDict): |
| a: int |
| b: str |
| |
| [case testNotTypeddict] |
| from x import TypedDict |
| import y |
| X = TypedDict('X', {'a': int, 'b': str}) |
| Y = y.TypedDict('Y', {'a': int, 'b': str}) |
| [out] |
| from _typeshed import Incomplete |
| |
| X: Incomplete |
| Y: Incomplete |
| |
| [case testTypeddictWithWrongAttributesType] |
| from typing import TypedDict |
| R = TypedDict("R", {"a": int, **{"b": str, "c": bytes}}) |
| S = TypedDict("S", [("b", str), ("c", bytes)]) |
| T = TypedDict("T", {"a": int}, b=str, total=False) |
| U = TypedDict("U", {"a": int}, totale=False) |
| V = TypedDict("V", {"a": int}, {"b": str}) |
| W = TypedDict("W", **{"a": int, "b": str}) |
| [out] |
| from _typeshed import Incomplete |
| |
| R: Incomplete |
| S: Incomplete |
| T: Incomplete |
| U: Incomplete |
| V: Incomplete |
| W: Incomplete |
| |
| [case testUseTypingName] |
| import collections |
| import typing |
| from typing import NamedTuple, TypedDict |
| |
| class Incomplete: ... |
| class Generator: ... |
| class NamedTuple: ... |
| class TypedDict: ... |
| |
| nt = collections.namedtuple("nt", "a b") |
| NT = typing.NamedTuple("NT", [("a", int), ("b", str)]) |
| NT1 = typing.NamedTuple("NT1", [("a", int)] + [("b", str)]) |
| NT2 = typing.NamedTuple("NT2", [(xx, int), ("b", str)]) |
| NT3 = typing.NamedTuple(xx, [("a", int), ("b", str)]) |
| TD = typing.TypedDict("TD", {"a": int, "b": str}) |
| TD1 = typing.TypedDict("TD1", {"a": int, "b": str}, totale=False) |
| TD2 = typing.TypedDict("TD2", {xx: int, "b": str}) |
| TD3 = typing.TypedDict(xx, {"a": int, "b": str}) |
| |
| def gen(): |
| y = yield x |
| return z |
| |
| def gen2(): |
| y = yield from x |
| return z |
| |
| class X(unknown_call("X", "a b")): ... |
| class Y(collections.namedtuple("Y", xx)): ... |
| [out] |
| from _typeshed import Incomplete as _Incomplete |
| from collections.abc import Generator as _Generator |
| from typing import NamedTuple as _NamedTuple |
| from typing_extensions import TypedDict as _TypedDict |
| |
| class Incomplete: ... |
| class Generator: ... |
| class NamedTuple: ... |
| class TypedDict: ... |
| |
| class nt(_NamedTuple): |
| a: _Incomplete |
| b: _Incomplete |
| |
| class NT(_NamedTuple): |
| a: int |
| b: str |
| |
| NT1: _Incomplete |
| NT2: _Incomplete |
| NT3: _Incomplete |
| |
| class TD(_TypedDict): |
| a: int |
| b: str |
| |
| TD1: _Incomplete |
| TD2: _Incomplete |
| TD3: _Incomplete |
| |
| def gen() -> _Generator[_Incomplete, _Incomplete, _Incomplete]: ... |
| def gen2() -> _Generator[_Incomplete, _Incomplete, _Incomplete]: ... |
| |
| class X(_Incomplete): ... |
| class Y(_Incomplete): ... |