Skip to content

Commit

Permalink
Added language to reflect the intended behavior for triple-quoted typ…
Browse files Browse the repository at this point in the history
…e annotation expressions. (#1578)

* Added language to reflect the intended behavior for triple-quoted type annotation expressions.

* Updated conformance tests to include new rules for triple-quoted annotations.
  • Loading branch information
erictraut authored Jan 15, 2024
1 parent c54c84d commit 0665b39
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions conformance/results/mypy/annotations_forward_refs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ notes = """
Does not report error for a forward reference that is not enclosed in quotes.
Does not report error for use of quoted type with "|" operator (runtime error).
Incorrectly generates error for quoted type defined in class scope.
Does not treat triple-quoted forward reference annotation as implicitly parenthesized.
"""
output = """
annotations_forward_refs.py:41: error: Invalid type comment or annotation [valid-type]
Expand All @@ -28,4 +29,5 @@ annotations_forward_refs.py:87: note: Perhaps you need "Callable[...]" or a call
annotations_forward_refs.py:89: error: Function "annotations_forward_refs.ClassD.int" is not valid as a type [valid-type]
annotations_forward_refs.py:89: note: Perhaps you need "Callable[...]" or a callback protocol?
annotations_forward_refs.py:93: error: Expression is of type int?, not "int" [assert-type]
annotations_forward_refs.py:100: error: Invalid type comment or annotation [valid-type]
"""
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.8.0"
test_duration = 1.3906440734863281
test_duration = 2.001461982727051
2 changes: 2 additions & 0 deletions conformance/results/pyre/annotations_forward_refs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Does not report error for use of quoted type with "|" operator (runtime error).
Does not reject f-string in quoted type annotation.
Incorrectly generates error for quoted type defined in class scope.
Does not generate error for unquoted type defined in class scope.
Does not treat triple-quoted forward reference annotation as implicitly parenthesized.
"""
output = """
annotations_forward_refs.py:19:25 Incompatible parameter type [6]: In call `typing.GenericMeta.__getitem__`, for 1st positional argument, expected `Type[Variable[_T]]` but got `UnionType`.
Expand All @@ -25,4 +26,5 @@ annotations_forward_refs.py:55:9 Undefined or invalid type [11]: Annotation `typ
annotations_forward_refs.py:77:0 Uninitialized attribute [13]: Attribute `ClassC` is declared in class `ClassD` to have type `ClassC` but is never initialized.
annotations_forward_refs.py:80:12 Undefined or invalid type [11]: Annotation `ClassF` is not defined as a type.
annotations_forward_refs.py:87:7 Undefined or invalid type [11]: Annotation `ClassD.int` is not defined as a type.
annotations_forward_refs.py:100:7 Undefined or invalid type [11]: Annotation `
"""
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.19"
test_duration = 2.216038227081299
test_duration = 2.3505892753601074
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.347"
test_duration = 1.3326058387756348
test_duration = 1.371502161026001
2 changes: 2 additions & 0 deletions conformance/results/pytype/annotations_forward_refs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ notes = """
Does not reject some illegal type expression forms when quoted.
Incorrectly generates error for quoted type defined in class scope.
Evaluates incorrect type for class variable annotated with quoted type expression.
Does not treat triple-quoted forward reference annotation as implicitly parenthesized.
"""
output = """
File "annotations_forward_refs.py", line 22, in <module>: Name 'ClassA' is not defined [name-error]
Expand Down Expand Up @@ -42,4 +43,5 @@ File "annotations_forward_refs.py", line 89, in ClassD: Invalid type annotation
File "annotations_forward_refs.py", line 93, in <module>: Any [assert-type]
Expected: int
Actual: Any
File "annotations_forward_refs.py", line 100, in <module>: unexpected indent [python-compiler-error]
"""
2 changes: 1 addition & 1 deletion conformance/results/pytype/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pytype 2023.12.18"
test_duration = 27.246526956558228
test_duration = 27.59779715538025
2 changes: 1 addition & 1 deletion conformance/results/results.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions conformance/tests/annotations_forward_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def int(self) -> None: # OK

assert_type(ClassD.str, str)
assert_type(ClassD.x, int)


# > If a triple quote is used, the string should be parsed as though it is implicitly
# > surrounded by parentheses. This allows newline characters to be
# > used within the string literal.

value: """
int |
str |
list[int]
"""
10 changes: 10 additions & 0 deletions docs/spec/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ be evaluated.
Moreover, the expression should be parseable as a valid type hint, i.e.,
it is constrained by the rules from the section on :ref:`valid-types`.

If a triple quote is used, the string should be parsed as though it is
implicitly surrounded by parentheses. This allows newline characters to be
used within the string literal::

value: """
int |
str |
list[Any]
"""

It is allowable to use string literals as *part* of a type hint, for
example::

Expand Down

0 comments on commit 0665b39

Please sign in to comment.