Merge pull request #2982 from fanninpm/fix-typing

Add test_typing from CPython 3.8
This commit is contained in:
Jim Fasarakis-Hilliard
2021-08-28 21:10:36 +03:00
committed by GitHub
7 changed files with 4191 additions and 24 deletions

60
Lib/test/ann_module.py Normal file
View File

@@ -0,0 +1,60 @@
"""
The module for testing variable annotations.
Empty lines above are for good reason (testing for correct line numbers)
"""
from typing import Optional
from functools import wraps
__annotations__[1] = 2
class C:
x = 5; y: Optional['C'] = None
from typing import Tuple
x: int = 5; y: str = x; f: Tuple[int, int]
class M(type):
__annotations__['123'] = 123
o: type = object
(pars): bool = True
class D(C):
j: str = 'hi'; k: str= 'bye'
from types import new_class
h_class = new_class('H', (C,))
j_class = new_class('J')
class F():
z: int = 5
def __init__(self, x):
pass
class Y(F):
def __init__(self):
super(F, self).__init__(123)
class Meta(type):
def __new__(meta, name, bases, namespace):
return super().__new__(meta, name, bases, namespace)
class S(metaclass = Meta):
x: str = 'something'
y: str = 'something else'
def foo(x: int = 10):
def bar(y: List[str]):
x: str = 'yes'
bar()
def dec(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper

36
Lib/test/ann_module2.py Normal file
View File

@@ -0,0 +1,36 @@
"""
Some correct syntax for variable annotation here.
More examples are in test_grammar and test_parser.
"""
from typing import no_type_check, ClassVar
i: int = 1
j: int
x: float = i/10
def f():
class C: ...
return C()
f().new_attr: object = object()
class C:
def __init__(self, x: int) -> None:
self.x = x
c = C(5)
c.new_attr: int = 10
__annotations__ = {}
@no_type_check
class NTC:
def meth(self, param: complex) -> None:
...
class CV:
var: ClassVar['CV']
CV.var = CV()

18
Lib/test/ann_module3.py Normal file
View File

@@ -0,0 +1,18 @@
"""
Correct syntax for variable annotation that should fail at runtime
in a certain manner. More examples are in test_grammar and test_parser.
"""
def f_bad_ann():
__annotations__[1] = 2
class C_OK:
def __init__(self, x: int) -> None:
self.x: no_such_name = x # This one is OK as proposed by Guido
class D_bad_ann:
def __init__(self, x: int) -> None:
sfel.y: int = 0
def g_bad_ann():
no_such_name.attr: int = 0

View File

@@ -0,0 +1,53 @@
"""Module for testing the behavior of generics across different modules."""
import sys
from textwrap import dedent
from typing import TypeVar, Generic, Optional
if sys.version_info[:2] >= (3, 6):
exec(dedent("""
default_a: Optional['A'] = None
default_b: Optional['B'] = None
T = TypeVar('T')
class A(Generic[T]):
some_b: 'B'
class B(Generic[T]):
class A(Generic[T]):
pass
my_inner_a1: 'B.A'
my_inner_a2: A
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__
"""))
else: # This should stay in sync with the syntax above.
__annotations__ = dict(
default_a=Optional['A'],
default_b=Optional['B'],
)
default_a = None
default_b = None
T = TypeVar('T')
class A(Generic[T]):
__annotations__ = dict(
some_b='B'
)
class B(Generic[T]):
class A(Generic[T]):
pass
__annotations__ = dict(
my_inner_a1='B.A',
my_inner_a2=A,
my_outer_a='A' # unless somebody calls get_type_hints with localns=B.__dict__
)

View File

@@ -399,8 +399,6 @@ class GrammarTests(unittest.TestCase):
{'123': 123, 'o': type})
self.assertEqual(ann_module2.__annotations__, {})
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_var_annot_in_module(self):
# check that functions fail the same way when executed
# outside of module where they were defined

4004
Lib/test/test_typing.py Normal file

File diff suppressed because it is too large Load Diff

42
Lib/typing.py vendored
View File

@@ -73,13 +73,13 @@ __all__ = [
# Structural checks, a.k.a. protocols.
'Reversible',
# 'SupportsAbs', # TODO: RUSTPYTHON
'SupportsAbs',
'SupportsBytes',
'SupportsComplex',
'SupportsFloat',
'SupportsIndex',
'SupportsInt',
# 'SupportsRound', # TODO: RUSTPYTHON
'SupportsRound',
# Concrete collection types.
'ChainMap',
@@ -1570,26 +1570,24 @@ class SupportsIndex(Protocol):
pass
# TODO: RUSTPYTHON, sort out the method resolution order nonsense
#
# @runtime_checkable
# class SupportsAbs(Protocol[T_co]):
# """An ABC with one abstract method __abs__ that is covariant in its return type."""
# __slots__ = ()
#
# @abstractmethod
# def __abs__(self) -> T_co:
# pass
#
#
# @runtime_checkable
# class SupportsRound(Protocol[T_co]):
# """An ABC with one abstract method __round__ that is covariant in its return type."""
# __slots__ = ()
#
# @abstractmethod
# def __round__(self, ndigits: int = 0) -> T_co:
# pass
@runtime_checkable
class SupportsAbs(Protocol[T_co]):
"""An ABC with one abstract method __abs__ that is covariant in its return type."""
__slots__ = ()
@abstractmethod
def __abs__(self) -> T_co:
pass
@runtime_checkable
class SupportsRound(Protocol[T_co]):
"""An ABC with one abstract method __round__ that is covariant in its return type."""
__slots__ = ()
@abstractmethod
def __round__(self, ndigits: int = 0) -> T_co:
pass
def _make_nmtuple(name, types):