Skip to content

Commit 0102ba1

Browse files
removed GenericMixin.type_var
1 parent 50ca878 commit 0102ba1

File tree

3 files changed

+6
-37
lines changed

3 files changed

+6
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- removed decorator `@timer_class`
1010
- removed decorator `@uminplemented`
1111
- removed decorator `@trace_if_returns`
12+
- removed `GenericMixin.type_var`. Use `GenericMixin.type_vars` instead.
1213
- added `Taskfile.yml` and use it in CI
1314
- removed `create_pdoc.sh`
1415
- moved `examples` out of the package

pedantic/mixins/generic_mixin.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ class GenericMixin:
1717
{~T: <class 'str'>, ~U: <class 'int'>}
1818
"""
1919

20-
@property
21-
def type_var(self) -> type:
22-
"""
23-
Get the type variable for this class.
24-
25-
Use this for convenience if your class has only one type parameter.
26-
27-
DO NOT call this inside __init__()!
28-
29-
Example:
30-
>>> from typing import Generic, TypeVar
31-
>>> T = TypeVar('T')
32-
>>> class Foo(Generic[T], GenericMixin):
33-
... value: T
34-
>>> f = Foo[float]()
35-
>>> f.type_var
36-
<class 'float'>
37-
"""
38-
39-
types = self._get_resolved_typevars()
40-
41-
if len(types) > 1:
42-
raise ValueError('You have multiple type parameters. Please use "type_vars" instead of "type_var".')
43-
44-
return next(iter(types.values()))
45-
4620
@property
4721
def type_vars(self) -> dict[TypeVar, type]:
4822
"""

tests/mixins/test_generic_mixin.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ class Foo(Generic[T], GenericMixin):
1616
value: T
1717

1818
foo = Foo[str]()
19-
assert foo.type_var == str
2019
assert foo.type_vars == {T: str}
2120

2221
invalid = Foo()
2322

2423
with pytest.raises(expected_exception=AssertionError) as err:
25-
invalid.type_var
24+
invalid.type_vars
2625

2726
assert f'You need to instantiate this class with type parameters! Example: Foo[int]()' in err.value.args[0]
2827

@@ -34,17 +33,12 @@ class Foo(Generic[T, U], GenericMixin):
3433

3534
foo = Foo[str, int]()
3635

37-
with pytest.raises(expected_exception=ValueError) as err:
38-
foo.type_var
39-
40-
assert 'You have multiple type parameters. Please use "type_vars" instead of "type_var".' in err.value.args[0]
41-
4236
assert foo.type_vars == {T: str, U: int}
4337

4438
invalid = Foo()
4539

4640
with pytest.raises(expected_exception=AssertionError) as err:
47-
invalid.type_var
41+
invalid.type_vars
4842

4943
assert 'You need to instantiate this class with type parameters! Example: Foo[int]()' in err.value.args[0]
5044

@@ -56,15 +50,15 @@ class Foo(GenericMixin):
5650
invalid = Foo()
5751

5852
with pytest.raises(expected_exception=AssertionError) as err:
59-
invalid.type_var
53+
invalid.type_vars
6054

6155
assert err.value.args[0] == 'Foo is not a generic class. To make it generic, declare it like: class Foo(Generic[T], GenericMixin):...'
6256

6357

64-
def test_call_type_var_in_constructor():
58+
def test_call_type_vars_in_constructor():
6559
class Foo(Generic[T], GenericMixin):
6660
def __init__(self) -> None:
67-
self.x = self.type_var()
61+
self.x = self.type_vars
6862

6963
with pytest.raises(expected_exception=AssertionError) as err:
7064
Foo[str]()

0 commit comments

Comments
 (0)