Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special-casing for __match_args__ fields #192

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## Unreleased

Behaviour changes:
* Add special-casing so that string literals are allowed in the context of
`__match_args__` assignments inside a class definition.
* Add special-casing so that arbitrary values can be assigned to a variable in
a stub file if the variable is annotated with `Final`.

## 22.2.0

Bugfixes:
Expand Down
12 changes: 10 additions & 2 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def _is_object(node: ast.expr, name: str, *, from_: Container[str]) -> bool:
_is_Any = partial(_is_object, name="Any", from_={"typing"})
_is_overload = partial(_is_object, name="overload", from_={"typing"})
_is_final = partial(_is_object, name="final", from_=_TYPING_MODULES)
_is_Final = partial(_is_object, name="Final", from_=_TYPING_MODULES)
_is_Self = partial(_is_object, name="Self", from_=({"_typeshed"} | _TYPING_MODULES))


Expand Down Expand Up @@ -622,7 +623,10 @@ def visit_Assign(self, node: ast.Assign) -> None:
else:
self.error(node, Y017)
target_name = None
if target_name == "__all__":
is_special_assignment = (
target_name == "__match_args__" and self.in_class.active
) or (target_name == "__all__" and not self.in_class.active)
if is_special_assignment:
with self.string_literals_allowed.enabled():
self.generic_visit(node)
else:
Expand All @@ -648,7 +652,7 @@ def visit_Assign(self, node: ast.Assign) -> None:
# We avoid triggering Y026 for calls and = ... because there are various
# unusual cases where assignment to the result of a call is legitimate
# in stubs.
if target_name != "__all__" and not isinstance(
if not is_special_assignment and not isinstance(
assignment, (ast.Ellipsis, ast.Call)
):
self.error(node, Y026)
Expand Down Expand Up @@ -694,6 +698,10 @@ def visit_Expr(self, node: ast.Expr) -> None:
self.generic_visit(node)

def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
if _is_Final(node.annotation):
with self.string_literals_allowed.enabled():
self.generic_visit(node)
return
if _is_name(node.target, "__all__") and not self.in_class.active:
with self.string_literals_allowed.enabled():
self.generic_visit(node)
Expand Down
22 changes: 21 additions & 1 deletion tests/attribute_annotations.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import TypeAlias
import typing
from typing import Final, TypeAlias

import typing_extensions

field1: int
field2: int = ...
Expand All @@ -13,6 +16,15 @@ field8 = False # Y015 Bad default value. Use "field8 = ..." instead of "field8
field9 = None # Y026 Use typing_extensions.TypeAlias for type aliases
field10: TypeAlias = None

# Tests for Final
field11: Final = 1
field12: Final = "foo"
field13: Final = b"foo"
field14: Final = True
field15: Final = ('a', 'b', 'c')
field16: typing.Final = "foo"
field17: typing_extensions.Final = "foo"

class Foo:
field1: int
field2: int = ...
Expand All @@ -22,3 +34,11 @@ class Foo:
field6 = 0 # Y015 Bad default value. Use "field6 = ..." instead of "field6 = 0"
field7 = b"" # Y015 Bad default value. Use "field7 = ..." instead of "field7 = b''"
field8 = False # Y015 Bad default value. Use "field8 = ..." instead of "field8 = False"
# Tests for Final
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no, I added tests here too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beat you to it ;)

field9: Final = 1
field10: Final = "foo"
field11: Final = b"foo"
field12: Final = True
field13: Final = ('a', 'b', 'c')
field14: typing.Final = "foo"
field15: typing_extensions.Final = "foo"
3 changes: 3 additions & 0 deletions tests/quotes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing import Annotated, Literal, TypeAlias, TypeVar
import typing_extensions

__all__ = ["f", "g"]
__match_args__ = ('foo',) # Y026 Use typing_extensions.TypeAlias for type aliases # Y020 Quoted annotations should never be used in stubs

def f(x: "int"): ... # Y020 Quoted annotations should never be used in stubs
def g(x: list["int"]): ... # Y020 Quoted annotations should never be used in stubs
Expand All @@ -18,3 +19,5 @@ Alias: TypeAlias = list["int"] # Y020 Quoted annotations should never be used i

class Child(list["int"]): # Y020 Quoted annotations should never be used in stubs
"""Documented and guaranteed useful.""" # Y021 Docstrings should not be included in stubs
__all__ = ('foo',) # Y026 Use typing_extensions.TypeAlias for type aliases # Y020 Quoted annotations should never be used in stubs
__match_args__ = ('foo', 'bar')