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

Fix type errors in Pyright #999

Merged
merged 18 commits into from Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/attr/__init__.pyi
Expand Up @@ -3,13 +3,11 @@ import sys
from typing import (
Any,
Callable,
ClassVar,
Dict,
Generic,
List,
Mapping,
Optional,
Protocol,
Sequence,
Tuple,
Type,
Expand All @@ -26,6 +24,9 @@ from . import setters as setters
from . import validators as validators
from ._cmp import cmp_using as cmp_using
from ._version_info import VersionInfo
from ._typing_compat import AttrsInstance_

AttrsInstance = AttrsInstance_
Tinche marked this conversation as resolved.
Show resolved Hide resolved

if sys.version_info >= (3, 10):
from typing import TypeGuard
Expand Down Expand Up @@ -64,10 +65,6 @@ _FieldTransformer = Callable[
# _ValidatorType from working when passed in a list or tuple.
_ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]

# A protocol to be able to statically accept an attrs class.
class AttrsInstance(Protocol):
__attrs_attrs__: ClassVar[Any]

# _make --

NOTHING: object
Expand Down
12 changes: 12 additions & 0 deletions src/attr/_typing_compat.pyi
@@ -0,0 +1,12 @@
from typing import Any, ClassVar, Protocol

MYPY = False

# A protocol to be able to statically accept an attrs class.
class AttrsInstance(Protocol):
__attrs_attrs__: ClassVar[Any]

if MYPY:
AttrsInstance_ = AttrsInstance
else:
AttrsInstance_ = Any
46 changes: 38 additions & 8 deletions tests/test_pyright.py
Expand Up @@ -11,6 +11,7 @@


_found_pyright = shutil.which("pyright")
pytestmark = pytest.mark.skipif(not _found_pyright, reason="Requires pyright.")


@attr.s(frozen=True)
Expand All @@ -19,14 +20,7 @@ class PyrightDiagnostic:
message = attr.ib()


@pytest.mark.skipif(not _found_pyright, reason="Requires pyright.")
def test_pyright_baseline():
"""The __dataclass_transform__ decorator allows pyright to determine
attrs decorated class types.
"""

test_file = os.path.dirname(__file__) + "/dataclass_transform_example.py"

def parse_pyright_output(test_file):
pyright = subprocess.run(
["pyright", "--outputjson", str(test_file)], capture_output=True
)
Expand All @@ -37,6 +31,17 @@ def test_pyright_baseline():
for d in pyright_result["generalDiagnostics"]
}

return diagnostics


def test_pyright_baseline():
"""The __dataclass_transform__ decorator allows pyright to determine
attrs decorated class types.
"""
diagnostics = parse_pyright_output(
os.path.dirname(__file__) + "/dataclass_transform_example.py"
)

# Expected diagnostics as per pyright 1.1.135
expected_diagnostics = {
PyrightDiagnostic(
Expand Down Expand Up @@ -65,3 +70,28 @@ def test_pyright_baseline():
}

assert diagnostics == expected_diagnostics


def test_pyright_attrsinstance_is_any(tmp_path):
"""
Test that `AttrsInstance` is `Any` under Pyright.
"""
test_pyright_attrsinstance_is_any_path = (
tmp_path / "test_pyright_attrsinstance_is_any.py"
)
test_pyright_attrsinstance_is_any_path.write_text(
"""\
import attrs

reveal_type(attrs.AttrsInstance)
"""
)

diagnostics = parse_pyright_output(test_pyright_attrsinstance_is_any_path)
expected_diagnostics = {
PyrightDiagnostic(
severity="information",
message='Type of "attrs.AttrsInstance" is "Any"',
),
}
assert diagnostics == expected_diagnostics
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -93,7 +93,7 @@ commands = towncrier build --version UNRELEASED --draft
basepython = python3.10
deps = mypy>=0.902
commands =
mypy src/attrs/__init__.pyi src/attr/__init__.pyi src/attr/_version_info.pyi src/attr/converters.pyi src/attr/exceptions.pyi src/attr/filters.pyi src/attr/setters.pyi src/attr/validators.pyi
mypy src/attrs/__init__.pyi src/attr/__init__.pyi src/attr/_typing_compat.pyi src/attr/_version_info.pyi src/attr/converters.pyi src/attr/exceptions.pyi src/attr/filters.pyi src/attr/setters.pyi src/attr/validators.pyi
mypy tests/typing_example.py


Expand Down