Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement pyright support via dataclass_transforms#796

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hynek merged 19 commits intopython-attrs:mainfromasford:dataclass_transforms
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
19 commits
Select commitHold shift + click to select a range
52d769c
Add __dataclass_transform__ decorator.
asfordApr 27, 2021
1891373
Add doc notes for pyright dataclass_transform support.
asfordApr 27, 2021
4f3787c
Fix docs build error.
asfordApr 28, 2021
bba9d7a
Expand docs on dataclass_transform
asfordApr 29, 2021
70e4286
Add changelog
asfordApr 29, 2021
41b4149
Fix docs build
asfordApr 29, 2021
8abde8a
Fix lint
asfordApr 29, 2021
2a4f8df
Add note on __dataclass_transform__ in .pyi
asfordApr 29, 2021
822722b
Add baseline test of pyright support via tox
asfordApr 29, 2021
55a0675
Add pyright tests to tox run configuration
asfordApr 29, 2021
7c21153
Fix test errors, enable in tox.
asfordApr 29, 2021
e8a4c0d
Fixup lint
asfordApr 29, 2021
6d10e30
Move pyright to py39
asfordApr 30, 2021
5fa3b2d
Add test docstring.
asfordApr 30, 2021
59714cf
Fixup docs.
asfordApr 30, 2021
68e86bf
Merge branch 'main' into dataclass_transforms
hynekMay 1, 2021
693e71b
Merge branch 'main' into dataclass_transforms
hynekMay 3, 2021
e63087b
Merge branch 'main' into dataclass_transforms
hynekMay 5, 2021
7876c40
Merge branch 'main' into dataclass_transforms
hynekMay 5, 2021
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
PrevPrevious commit
NextNext commit
Add baseline test of pyright support via tox
  • Loading branch information
@asford
asford committedApr 29, 2021
commit822722b416913f6abc6e276285a0b04d54e56070
44 changes: 44 additions & 0 deletionstests/dataclass_transform_example.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
from typing import Optional

import attr


@attr.define()
class Define:
a: str
b: int


reveal_type(Define.__init__)


@attr.define()
class DefineConverter:
# mypy plugin adapts the "int" method signature, pyright does not
with_converter: int = attr.field(converter=int)


reveal_type(DefineConverter.__init__)


# mypy plugin supports attr.frozen, pyright does not
@attr.frozen()
class Frozen:
a: str


d = Frozen("a")
d.a = "new"

reveal_type(d.a)

# but pyright supports attr.define(frozen)
@attr.define(frozen=True)
class FrozenDefine:
a: str


d2 = FrozenDefine("a")
d2.a = "new"

reveal_type(d2.a)
63 changes: 63 additions & 0 deletionstests/test_pyright.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
import json
import pathlib
import shutil
import subprocess
import sys

import pytest

import attr


if sys.version_info < (3, 6):
_found_pyright = False
else:
_found_pyright = shutil.which("pyright")


@attr.define(frozen=True)
class PyrightDiagnostic:
severity: str
message: str


@pytest.mark.skipif(not _found_pyright, reason="Requires pyright.")
def test_pyright_baseline():
test_file = (
pathlib.Path(__file__).parent / "dataclass_transform_example.py"
)

pyright = subprocess.run(
["pyright", "--outputjson", str(test_file)], capture_output=True
)
pyright_result = json.loads(pyright.stdout)

diagnostics = set(
PyrightDiagnostic(d["severity"], d["message"])
for d in pyright_result["generalDiagnostics"]
)

expected_diagnostics = {
PyrightDiagnostic(
severity="information",
message='Type of "Define.__init__" is "(self: Define, a: str, b: int) -> None"',
),
PyrightDiagnostic(
severity="information",
message='Type of "DefineConverter.__init__" is "(self: DefineConverter, with_converter: int) -> None"',
),
PyrightDiagnostic(
severity="information",
message='Type of "d.a" is "Literal[\'new\']"',
),
PyrightDiagnostic(
severity="error",
message='Cannot assign member "a" for type "FrozenDefine"\n\xa0\xa0"FrozenDefine" is frozen',
),
PyrightDiagnostic(
severity="information",
message='Type of "d2.a" is "Literal[\'new\']"',
),
}

assert diagnostics == expected_diagnostics
14 changes: 14 additions & 0 deletionstox.ini
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,3 +121,17 @@ deps = mypy>=0.800
commands =
mypy 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 tests/typing_example.py


[testenv:pyright]
# Install and configure node and pyright
# This *could* be folded into a custom install_command
# Use nodeenv to configure node in the running tox virtual environment
# Seeing errors using "nodeenv -p"
# Use npm install -g to install "globally" into the virtual environment
basepython = python3.8
deps = nodeenv
commands =
nodeenv --prebuilt --node=lts --force {envdir}
npm install -g --no-package-lock --no-save pyright@1.1.135
pytest tests/test_pyright.py -vv

[8]ページ先頭

©2009-2025 Movatter.jp