diff options
author | da-woods <dw-git@d-woods.co.uk> | 2022-07-31 17:34:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-31 17:34:43 +0100 |
commit | f6edb652db32e12505be8bc4767e64b50790f378 (patch) | |
tree | 7ae88b37e7568c4559ce6f2250a2859ff0c0bd59 /tests | |
parent | 6cd64875336b5121e145a3fe85c7621f3db5a64e (diff) | |
download | cython-f6edb652db32e12505be8bc4767e64b50790f378.tar.gz |
Add var_entry for "special" modules (e.g. typing) (#4603)
Allows them to be accessed as regular Python attributes too.
(First second or third(?) fix for something that probably should have been in #3400)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/run/cdef_class_dataclass.pyx | 9 | ||||
-rw-r--r-- | tests/run/pep526_variable_annotations.py | 24 |
2 files changed, 31 insertions, 2 deletions
diff --git a/tests/run/cdef_class_dataclass.pyx b/tests/run/cdef_class_dataclass.pyx index 2f69e0f8f..7be88f695 100644 --- a/tests/run/cdef_class_dataclass.pyx +++ b/tests/run/cdef_class_dataclass.pyx @@ -224,6 +224,10 @@ cdef class TestFrozen: """ a: cython.double = 2.0 +def get_dataclass_initvar(): + return py_dataclasses.InitVar + + @dataclass(kw_only=True) cdef class TestKwOnly: """ @@ -251,10 +255,11 @@ cdef class TestKwOnly: a: cython.double = 2.0 b: cython.long + import sys if sys.version_info >= (3, 7): __doc__ = """ - >>> from dataclasses import Field, is_dataclass, fields + >>> from dataclasses import Field, is_dataclass, fields, InitVar # It uses the types from the standard library where available >>> all(isinstance(v, Field) for v in BasicDataclass.__dataclass_fields__.values()) @@ -275,4 +280,6 @@ if sys.version_info >= (3, 7): ['a', 'b', 'c', 'd'] >>> [ f.name for f in fields(InitClassVars)] ['a'] + >>> get_dataclass_initvar() == InitVar + True """ diff --git a/tests/run/pep526_variable_annotations.py b/tests/run/pep526_variable_annotations.py index 56cb0201b..6f430c0af 100644 --- a/tests/run/pep526_variable_annotations.py +++ b/tests/run/pep526_variable_annotations.py @@ -203,6 +203,29 @@ def test_tuple(a: typing.Tuple[cython.int, cython.float], b: typing.Tuple[cython print(cython.typeof(c) + (" object" if not cython.compiled else "")) +def test_use_typing_attributes_as_non_annotations(): + """ + >>> test_use_typing_attributes_as_non_annotations() + typing.Tuple typing.Tuple[int] + typing.Optional True + typing.Optional True + """ + x1 = typing.Tuple + x2 = typing.Tuple[int] + y1 = typing.Optional + y2 = typing.Optional[typing.Dict] + z1 = Optional + z2 = Optional[Dict] + # The result of printing "Optional[type]" is slightly version-dependent + # so accept both possible forms + allowed_optional_strings = [ + "typing.Union[typing.Dict, NoneType]", + "typing.Optional[typing.Dict]" + ] + print(x1, x2) + print(y1, str(y2) in allowed_optional_strings) + print(z1, str(z2) in allowed_optional_strings) + if cython.compiled: __doc__ = """ # passing non-dicts to variables declared as dict now fails @@ -219,6 +242,5 @@ if cython.compiled: TypeError: Expected dict, got D """ - _WARNINGS = """ """ |