From: Vladimír Vondruš Date: Sun, 2 Jan 2022 20:53:34 +0000 (+0100) Subject: documentation/python: implicit TypeVar for typing.List was only in 3.7/8. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=2888f193418379491d8b3433477f085098790a34;p=blog.git documentation/python: implicit TypeVar for typing.List was only in 3.7/8. If I say typing.List, only Python 3.7 and 3.8 converted that to typing.List[T], 3.6, 3.9 and 3.10 keep typing.List. --- diff --git a/documentation/test_python/inspect_annotations/inspect_annotations-py37+38.html b/documentation/test_python/inspect_annotations/inspect_annotations-py37+38.html new file mode 100644 index 00000000..a8750e3f --- /dev/null +++ b/documentation/test_python/inspect_annotations/inspect_annotations-py37+38.html @@ -0,0 +1,181 @@ + + + + + inspect_annotations | My Python Project + + + + + +
+
+
+
+
+

+ inspect_annotations module +

+

Annotation parsing. For links inside annotations see test_inspect.TypeLinks.

+
+

Contents

+ +
+
+

Classes

+
+
class AContainer
+
A generic class. No parent class info extracted yet.
+
class AContainer2
+
Another class derived from a typing thing.
+
class Foo
+
A class with properties
+
class FooSlots
+
A class with slots
+
+
+
+

Functions

+
+
+ def annotated_positional_keyword(bar = False, *, + foo: str, + **kwargs) +
+
Function with explicitly delimited keyword args and type annotations
+
+ def annotation(param: typing.List[int], + another: bool, + third: str = 'hello') -> float +
+
An annotated function
+
+ def annotation_any(a: typing.Any) +
+
Annotation with the Any type
+
+ def annotation_callable(a: typing.Callable[[float, int], str]) +
+
Annotation with the Callable type
+
+ def annotation_callable_no_args(a: typing.Callable[[], typing.Dict[int, float]]) +
+
Annotation with the Callable type w/o arguments
+
+ def annotation_ellipsis(a: typing.Callable[[...], int], + b: typing.Tuple[str, ...]) +
+
Annotation with ellipsis
+
+ def annotation_func_instead_of_type(a) +
+
Annotation with a function instead of a type, ignored
+
+ def annotation_func_instead_of_type_nested(a, b, c) +
+
Annotations with nested problems, ignoring the whole thing
+
+ def annotation_generic(a: typing.List[Tp]) -> Tp +
+
Annotation with a generic type
+
+ def annotation_list_noparam(a: typing.List[T]) +
+
Annotation with the unparametrized List type. 3.7 and 3.8 adds an implicit TypeVar to it, 3.6, 3.9 and 3.10 not, so the output is different between the versions.
+
+ def annotation_optional(a: typing.Optional[float]) +
+
Annotation with the Optional type
+
+ def annotation_strings(param: typing.List[int], + another: bool, + third: str = 'hello') -> float +
+
Annotated using strings, should result in exactly the same as annotation()
+
+ def annotation_tuple_instead_of_tuple(a) +
+
Annotation with a tuple instead of Tuple, ignored
+
+ def annotation_union(a: typing.Union[float, int]) +
+
Annotation with the Union type
+
+ def annotation_union_of_undefined(a: typing.Union[int, something.Undefined]) +
+
Annotation with an union that has an undefined type inside, where we can't use isinstance either
+
+ def annotation_union_second_bracketed(a: typing.Union[float, typing.List[int]]) +
+
Annotation with the Union type and second type bracketed, where we can't use isinstance
+
+ def args_kwargs(a, b, *args, **kwargs) +
+
Function with args and kwargs
+
+ def no_annotation(a, b, z) +
+
Non-annotated function
+
+ def no_annotation_default_param(param, + another, + third = 'hello') +
+
Non-annotated function with a default parameter
+
+ def partial_annotation(foo, + param: typing.Tuple[int, int], + unannotated, + cls: object) +
+
Partially annotated function
+
+ def positional_keyword(positional_kw, *, kw_only) +
+
Function with explicitly delimited keyword args
+
+ def returns_none(a: typing.Callable[[], None]) -> None +
+
In order to disambiguate between a missing return annotation and an +annotated none, the None return annotation is kept, converted from NoneType +to None
+
+ def returns_none_type(a: typing.Callable[[], None]) -> None +
+
And it should behave the same when using None or type(None)
+
+
+
+

Data

+
+
+ ANNOTATED_VAR: typing.Tuple[bool, str] = (False, 'No.') +
+
+
+ UNANNOTATED_VAR = 3.45 +
+
+
+
+
+
+
+
+ + diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.html b/documentation/test_python/inspect_annotations/inspect_annotations.html index 494ccc15..78e96c36 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.html +++ b/documentation/test_python/inspect_annotations/inspect_annotations.html @@ -94,9 +94,9 @@
Annotation with a generic type
- def annotation_list_noparam(a: typing.List[T]) + def annotation_list_noparam(a: typing.List)
-
Annotation with the unparametrized List type. 3.7 adds an implicit TypeVar to it, 3.6 not, emulate that to make the test pass on older versions
+
Annotation with the unparametrized List type. 3.7 and 3.8 adds an implicit TypeVar to it, 3.6, 3.9 and 3.10 not, so the output is different between the versions.
def annotation_optional(a: typing.Optional[float])
diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.py b/documentation/test_python/inspect_annotations/inspect_annotations.py index c9b1cf20..a03d50b4 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.py +++ b/documentation/test_python/inspect_annotations/inspect_annotations.py @@ -75,9 +75,7 @@ def annotation_union_of_undefined(a: Union[int, 'something.Undefined']): """Annotation with an union that has an undefined type inside, where we can't use isinstance either""" def annotation_list_noparam(a: List): - """Annotation with the unparametrized List type. 3.7 adds an implicit TypeVar to it, 3.6 not, emulate that to make the test pass on older versions""" -if sys.version_info < (3, 7): - annotation_list_noparam.__annotations__['a'] = List[TypeVar('T')] + """Annotation with the unparametrized List type. 3.7 and 3.8 adds an implicit TypeVar to it, 3.6, 3.9 and 3.10 not, so the output is different between the versions.""" def annotation_generic(a: List[_T]) -> _T: """Annotation with a generic type""" diff --git a/documentation/test_python/test_inspect.py b/documentation/test_python/test_inspect.py index c96bcdf0..e8823904 100644 --- a/documentation/test_python/test_inspect.py +++ b/documentation/test_python/test_inspect.py @@ -85,7 +85,10 @@ class AllProperty(BaseInspectTestCase): class Annotations(BaseInspectTestCase): def test(self): self.run_python() - self.assertEqual(*self.actual_expected_contents('inspect_annotations.html')) + if LooseVersion(sys.version) >= LooseVersion('3.7.0') and LooseVersion(sys.version) < LooseVersion('3.9.0'): + self.assertEqual(*self.actual_expected_contents('inspect_annotations.html', 'inspect_annotations-py37+38.html')) + else: + self.assertEqual(*self.actual_expected_contents('inspect_annotations.html')) self.assertEqual(*self.actual_expected_contents('inspect_annotations.Foo.html')) self.assertEqual(*self.actual_expected_contents('inspect_annotations.FooSlots.html'))