GitHub

@@ -940,18 +940,17 @@ def g(x: int) -> int: ...

940940

def test_ellipsis(self):

941941

ty, errors = self.InferWithErrors("""

942942

from typing import Dict, Tuple

943-

def f(x: ...): pass # invalid-annotation[e1]

943+

def f(x: ...): pass # experimental "inferred type": see b/213607272

944944

def g(x: Tuple[str, ...]): pass

945-

def h(x: Dict[..., int]): pass # invalid-annotation[e2]

945+

def h(x: Dict[..., int]): pass # invalid-annotation[e]

946946

""")

947947

self.assertTypesMatchPytd(ty, """

948948

from typing import Any, Dict, Tuple

949949

def f(x) -> None: ...

950950

def g(x: Tuple[str, ...]) -> None: ...

951951

def h(x: Dict[Any, int]) -> None: ...

952952

""")

953-

self.assertErrorRegexes(

954-

errors, {"e1": r"Ellipsis.*x", "e2": r"Ellipsis.*Dict"})

953+

self.assertErrorRegexes(errors, {"e": r"Ellipsis.*Dict"})

955954956955

def test_custom_container(self):

957956

with file_utils.Tempdir() as d:

@@ -1298,5 +1297,42 @@ class B:

12981297

assert_type(A().c, Optional[B])

12991298

""")

130012991300+1301+

class EllipsisTest(test_base.BaseTest):

1302+

"""Tests usage of '...' to mean "inferred type".

1303+1304+

This is an experimental feature that makes it possible to explicitly annotate

1305+

a type as inferred. See b/213607272.

1306+

"""

1307+1308+

def test_variable(self):

1309+

ty = self.Infer("x: ... = 0")

1310+

self.assertTypesMatchPytd(ty, "x: int")

1311+1312+

def test_function(self):

1313+

ty = self.Infer("""

1314+

def f(x: ...) -> ...:

1315+

return x

1316+

""")

1317+

self.assertTypesMatchPytd(ty, """

1318+

from typing import TypeVar

1319+

_T0 = TypeVar('_T0')

1320+

def f(x: _T0) -> _T0: ...

1321+

""")

1322+1323+

def test_class(self):

1324+

ty = self.Infer("""

1325+

class Foo:

1326+

x: ...

1327+

def f(self):

1328+

self.x = 5

1329+

""")

1330+

self.assertTypesMatchPytd(ty, """

1331+

class Foo:

1332+

x: int

1333+

def f(self) -> None: ...

1334+

""")

1335+1336+13011337

if __name__ == "__main__":

13021338

test_base.main()

Read the original on github.com ↗