Skip to content

Commit 1352720

Browse files
committed
Make inspect/ClassProps NG-only
1 parent c9ae16c commit 1352720

6 files changed

Lines changed: 66 additions & 70 deletions

File tree

src/attr/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
from . import converters, exceptions, filters, setters, validators
1111
from ._cmp import cmp_using
1212
from ._config import get_run_validators, set_run_validators
13-
from ._funcs import asdict, assoc, astuple, has, inspect, resolve_types
13+
from ._funcs import asdict, assoc, astuple, has, resolve_types
1414
from ._make import (
1515
NOTHING,
1616
Attribute,
17-
ClassProps,
1817
Converter,
1918
Factory,
2019
_Nothing,
@@ -45,7 +44,6 @@ class AttrsInstance(Protocol):
4544
"NOTHING",
4645
"Attribute",
4746
"AttrsInstance",
48-
"ClassProps",
4947
"Converter",
5048
"Factory",
5149
"NothingType",
@@ -69,7 +67,6 @@ class AttrsInstance(Protocol):
6967
"get_run_validators",
7068
"has",
7169
"ib",
72-
"inspect",
7370
"make_class",
7471
"mutable",
7572
"resolve_types",

src/attr/__init__.pyi

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -141,58 +141,6 @@ class Attribute(Generic[_T]):
141141

142142
def evolve(self, **changes: Any) -> "Attribute[Any]": ...
143143

144-
class ClassProps:
145-
"""
146-
XXX: somehow defining/using enums Mypy starts looking at our own code
147-
and causes tons of errors.
148-
"""
149-
150-
# class Hashability(enum.Enum): ...
151-
# class KeywordOnly(enum.Enum): ...
152-
153-
is_exception: bool
154-
is_slotted: bool
155-
has_weakref_slot: bool
156-
is_frozen: bool
157-
# kw_only: ClassProps.KeywordOnly
158-
kw_only: Any
159-
collected_fields_by_mro: bool
160-
added_init: bool
161-
added_repr: bool
162-
added_eq: bool
163-
added_ordering: bool
164-
# hashability: ClassProps.Hashability
165-
hashability: Any
166-
added_match_args: bool
167-
added_str: bool
168-
added_pickling: bool
169-
on_setattr_hook: _OnSetAttrType | None
170-
field_transformer: Callable[[Attribute[Any]], Attribute[Any]] | None
171-
172-
def __init__(
173-
self,
174-
is_exception: bool,
175-
is_slotted: bool,
176-
has_weakref_slot: bool,
177-
is_frozen: bool,
178-
# kw_only: ClassProps.KeywordOnly
179-
kw_only: Any,
180-
collected_fields_by_mro: bool,
181-
added_init: bool,
182-
added_repr: bool,
183-
added_eq: bool,
184-
added_ordering: bool,
185-
# hashability: ClassProps.Hashability
186-
hashability: Any,
187-
added_match_args: bool,
188-
added_str: bool,
189-
added_pickling: bool,
190-
on_setattr_hook: _OnSetAttrType,
191-
field_transformer: Callable[[Attribute[Any]], Attribute[Any]],
192-
) -> None: ...
193-
@property
194-
def is_hashable(self) -> bool: ...
195-
196144
# NOTE: We had several choices for the annotation to use for type arg:
197145
# 1) Type[_T]
198146
# - Pros: Handles simple cases correctly
@@ -427,7 +375,6 @@ def astuple(
427375
retain_collection_types: bool = ...,
428376
) -> tuple[Any, ...]: ...
429377
def has(cls: type) -> TypeGuard[type[AttrsInstance]]: ...
430-
def inspect(cls: type) -> ClassProps: ...
431378
def assoc(inst: _T, **changes: Any) -> _T: ...
432379
def evolve(inst: _T, **changes: Any) -> _T: ...
433380

src/attrs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
NOTHING,
55
Attribute,
66
AttrsInstance,
7-
ClassProps,
87
Converter,
98
Factory,
109
NothingType,
@@ -18,12 +17,13 @@
1817
fields_dict,
1918
frozen,
2019
has,
21-
inspect,
2220
make_class,
2321
mutable,
2422
resolve_types,
2523
validate,
2624
)
25+
from attr._funcs import inspect
26+
from attr._make import ClassProps
2727
from attr._next_gen import asdict, astuple
2828

2929
from . import converters, exceptions, filters, setters, validators

src/attrs/__init__.pyi

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ from attr import validate as validate
4141
from attr import validators as validators
4242
from attr import attrib, asdict as asdict, astuple as astuple
4343
from attr import NothingType as NothingType
44-
from attr import ClassProps as ClassProps
45-
from attr import inspect as inspect
4644

4745
if sys.version_info >= (3, 11):
4846
from typing import dataclass_transform
@@ -263,3 +261,57 @@ def frozen(
263261
field_transformer: _FieldTransformer | None = ...,
264262
match_args: bool = ...,
265263
) -> Callable[[_C], _C]: ...
264+
265+
class ClassProps:
266+
"""
267+
XXX: somehow defining/using enums Mypy starts looking at our own code
268+
and causes tons of errors.
269+
"""
270+
271+
# class Hashability(enum.Enum): ...
272+
# class KeywordOnly(enum.Enum): ...
273+
274+
is_exception: bool
275+
is_slotted: bool
276+
has_weakref_slot: bool
277+
is_frozen: bool
278+
# kw_only: ClassProps.KeywordOnly
279+
kw_only: Any
280+
collected_fields_by_mro: bool
281+
added_init: bool
282+
added_repr: bool
283+
added_eq: bool
284+
added_ordering: bool
285+
# hashability: ClassProps.Hashability
286+
hashability: Any
287+
added_match_args: bool
288+
added_str: bool
289+
added_pickling: bool
290+
on_setattr_hook: _OnSetAttrType | None
291+
field_transformer: Callable[[Attribute[Any]], Attribute[Any]] | None
292+
293+
def __init__(
294+
self,
295+
is_exception: bool,
296+
is_slotted: bool,
297+
has_weakref_slot: bool,
298+
is_frozen: bool,
299+
# kw_only: ClassProps.KeywordOnly
300+
kw_only: Any,
301+
collected_fields_by_mro: bool,
302+
added_init: bool,
303+
added_repr: bool,
304+
added_eq: bool,
305+
added_ordering: bool,
306+
# hashability: ClassProps.Hashability
307+
hashability: Any,
308+
added_match_args: bool,
309+
added_str: bool,
310+
added_pickling: bool,
311+
on_setattr_hook: _OnSetAttrType,
312+
field_transformer: Callable[[Attribute[Any]], Attribute[Any]],
313+
) -> None: ...
314+
@property
315+
def is_hashable(self) -> bool: ...
316+
317+
def inspect(cls: type) -> ClassProps: ...

tests/test_funcs.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from attr import asdict, assoc, astuple, evolve, fields, has
2020
from attr._compat import Mapping, Sequence
21-
from attr.exceptions import AttrsAttributeNotFoundError, NotAnAttrsClassError
21+
from attr.exceptions import AttrsAttributeNotFoundError
2222
from attr.validators import instance_of
2323

2424
from .strategies import nested_classes, simple_classes
@@ -844,11 +844,3 @@ class C:
844844
inst: int
845845

846846
assert C(42) == evolve(C(23), inst=42)
847-
848-
849-
def test_inspect_not_attrs_class():
850-
"""
851-
inspect() raises an error if the class is not an attrs class.
852-
"""
853-
with pytest.raises(NotAnAttrsClassError):
854-
attr.inspect(object)

tests/test_next_gen.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,11 @@ def test_validators(self):
573573
from attrs.validators import and_
574574

575575
assert and_ is _attr.validators.and_
576+
577+
578+
def test_inspect_not_attrs_class():
579+
"""
580+
inspect() raises an error if the class is not an attrs class.
581+
"""
582+
with pytest.raises(attrs.exceptions.NotAnAttrsClassError):
583+
attrs.inspect(object)

0 commit comments

Comments
 (0)