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

Appearance settings

Commit ce67df1

Browse filesBrowse files
committed
Conditionally define the protocol instead
1 parent c227b2f commit ce67df1
Copy full SHA for ce67df1

File tree

Expand file treeCollapse file tree

3 files changed

+13
-9
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+13
-9
lines changed

‎src/attr/__init__.pyi

Copy file name to clipboardExpand all lines: src/attr/__init__.pyi
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from typing import (
88
List,
99
Mapping,
1010
Optional,
11+
Protocol,
1112
Sequence,
1213
Tuple,
1314
Type,
@@ -26,8 +27,6 @@ from ._cmp import cmp_using as cmp_using
2627
from ._version_info import VersionInfo
2728
from ._typing_compat import AttrsInstance_
2829

29-
AttrsInstance = AttrsInstance_
30-
3130
if sys.version_info >= (3, 10):
3231
from typing import TypeGuard
3332
else:
@@ -65,6 +64,10 @@ _FieldTransformer = Callable[
6564
# _ValidatorType from working when passed in a list or tuple.
6665
_ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
6766

67+
# We subclass this here to keep the protocol's qualified name clean.
68+
class AttrsInstance(AttrsInstance_, Protocol):
69+
pass
70+
6871
# _make --
6972

7073
NOTHING: object

‎src/attr/_typing_compat.pyi

Copy file name to clipboardExpand all lines: src/attr/_typing_compat.pyi
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ from typing import Any, ClassVar, Protocol
22

33
MYPY = False
44

5-
# A protocol to be able to statically accept an attrs class.
6-
class AttrsInstance(Protocol):
7-
__attrs_attrs__: ClassVar[Any]
8-
95
if MYPY:
10-
AttrsInstance_ = AttrsInstance
6+
# A protocol to be able to statically accept an attrs class.
7+
class AttrsInstance_(Protocol):
8+
__attrs_attrs__: ClassVar[Any]
9+
1110
else:
12-
AttrsInstance_ = Any
11+
class AttrsInstance_(Protocol):
12+
pass

‎tests/test_pyright.py

Copy file name to clipboardExpand all lines: tests/test_pyright.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def test_pyright_attrsinstance_is_any(tmp_path):
9191
"""\
9292
import attrs
9393
94+
foo: attrs.AttrsInstance = object() # We can assign any old object to `AttrsInstance`.
9495
reveal_type(attrs.AttrsInstance)
9596
"""
9697
)
@@ -99,7 +100,7 @@ def test_pyright_attrsinstance_is_any(tmp_path):
99100
expected_diagnostics = {
100101
PyrightDiagnostic(
101102
severity="information",
102-
message='Type of "attrs.AttrsInstance" is "Any"',
103+
message='Type of "attrs.AttrsInstance" is "Type[AttrsInstance]"',
103104
),
104105
}
105106
assert diagnostics == expected_diagnostics

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.