Skip to content

Commit c96769a

Browse files
committed
Fix pylance type errors
also remove class attrs of Registerer. bump patch version.
1 parent cd0eda9 commit c96769a

File tree

6 files changed

+35
-38
lines changed

6 files changed

+35
-38
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ Instead of this, which violates the Open-Closed Principle (OCP):
3737
```python
3838

3939

40-
def hello(args):
40+
def hello2(args):
4141
return "hello to you too"
4242

4343

44-
def info(args):
44+
def info2(args):
4545
return "how can i help you?"
4646

4747

48-
def play(args):
48+
def play2(args):
4949
return "let me play a song for you"
5050

5151

5252
def command_handler(command, args):
5353
if command == "hello":
54-
return hello(args)
54+
return hello2(args)
5555
if command == "info":
56-
return info(args)
56+
return info2(args)
5757
if command == "play":
58-
return play(args)
58+
return play2(args)
5959

6060

6161
command = "info"
@@ -79,10 +79,11 @@ let's create a registry:
7979
```python
8080

8181
import abc
82+
import typing
8283

8384

8485
class Animal(abc.ABC):
85-
is_wild: bool = None
86+
is_wild: typing.Optional[bool] = None
8687

8788
@abc.abstractmethod
8889
def walk(self):
@@ -117,7 +118,7 @@ class Sheep(Animal):
117118

118119

119120
# use your custom slug as unique identifier:
120-
@animal_registry.register("kitty")
121+
@animal_registry.register("mamad")
121122
class Cat(Animal):
122123
is_wild = False
123124

@@ -229,7 +230,7 @@ Here is all the things you can do with the `Registerer` class:
229230
### <kbd>class</kbd> `Registerer`
230231
A utility that can be used to create a registry object to register class or functions.
231232

232-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L22"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
233+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L17"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
233234

234235
### <kbd>method</kbd> `Registerer.__init__`
235236

@@ -266,7 +267,7 @@ get actual registered items as list (classes or functions)
266267

267268
---
268269

269-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L61"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
270+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L56"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
270271

271272
#### <kbd>method</kbd> `Registerer.is_registered`
272273

@@ -278,12 +279,12 @@ is the slug registered?
278279

279280
---
280281

281-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L125"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
282+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L120"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
282283

283284
#### <kbd>method</kbd> `Registerer.register`
284285

285286
```python
286-
register(item_or_custom_slug: Union[~T, str] = None, **kwargs)
287+
register(item_or_custom_slug: Optional[~T, str] = None, **kwargs)
287288
```
288289

289290
register a class or item to the registry
@@ -321,7 +322,7 @@ assert postgresql_connection.env == "prod"
321322

322323
---
323324

324-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L76"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
325+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L71"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
325326

326327
#### <kbd>method</kbd> `Registerer.validate`
327328

docgen/README.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ def play(args):
3737
"""
3838

3939

40-
def hello(args):
40+
def hello2(args):
4141
return "hello to you too"
4242

4343

44-
def info(args):
44+
def info2(args):
4545
return "how can i help you?"
4646

4747

48-
def play(args):
48+
def play2(args):
4949
return "let me play a song for you"
5050

5151

5252
def command_handler(command, args):
5353
if command == "hello":
54-
return hello(args)
54+
return hello2(args)
5555
if command == "info":
56-
return info(args)
56+
return info2(args)
5757
if command == "play":
58-
return play(args)
58+
return play2(args)
5959

6060

6161
command = "info"
@@ -79,10 +79,11 @@ def command_handler(command, args):
7979
"""
8080

8181
import abc
82+
import typing
8283

8384

8485
class Animal(abc.ABC):
85-
is_wild: bool = None
86+
is_wild: typing.Optional[bool] = None
8687

8788
@abc.abstractmethod
8889
def walk(self):
@@ -117,7 +118,7 @@ def walk(self):
117118

118119

119120
# use your custom slug as unique identifier:
120-
@animal_registry.register("kitty")
121+
@animal_registry.register("mamad")
121122
class Cat(Animal):
122123
is_wild = False
123124

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "registerer"
3-
version = "0.4.2"
3+
version = "0.4.3"
44
description = "Everything you need to implement maintainable and easy to use registry patterns in your project."
55
authors = ["Danial Keimasi <danialkeimasi@gmail.com>"]
66
license = "MIT"

registerer/registry.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
)
99
from registerer.validators import RegistryValidator
1010

11-
T = typing.TypeVar("T")
11+
T = typing.TypeVar("T", typing.Type, typing.Any)
1212

1313

1414
class Registerer(typing.Generic[T]):
1515
"""A utility that can be used to create a registry object to register class or functions."""
1616

17-
_registry_dict: typing.Dict[str, T] = None
18-
parent_class: typing.Optional[T] = None
19-
max_size: typing.Optional[int] = None
20-
validators: typing.Optional[typing.List] = None
21-
2217
def __init__(
2318
self,
2419
parent_class: typing.Optional[T] = None,
@@ -39,10 +34,10 @@ def __init__(
3934
Raises:
4035
RegistryCreationError: Can't create proper registry object.
4136
"""
42-
self._registry_dict = {}
43-
self.parent_class = parent_class if parent_class else self.parent_class
44-
self.max_size = max_size
45-
self.validators = (validators if validators else []) + (self.validators if self.validators else [])
37+
self._registry_dict: typing.Dict[str, T] = {}
38+
self.parent_class: typing.Optional[T] = parent_class
39+
self.max_size: typing.Optional[int] = max_size
40+
self.validators: typing.List = validators if validators else []
4641

4742
if self.max_size is not None and (not isinstance(self.max_size, int) or self.max_size <= 0):
4843
raise RegistryCreationError("max_size should be a int bigger than zero or None.")
@@ -94,7 +89,7 @@ def validate(self, item: T):
9489
for validator in self.validators:
9590
validator(item)
9691

97-
def __register(self, custom_slug: str = None, **kwargs):
92+
def __register(self, custom_slug: typing.Optional[str] = None, **kwargs):
9893
"""the inner function that handles register
9994
10095
Args:
@@ -122,7 +117,7 @@ def _wrapper_function(item: T):
122117

123118
return _wrapper_function
124119

125-
def register(self, item_or_custom_slug: typing.Union[T, str] = None, **kwargs):
120+
def register(self, item_or_custom_slug: typing.Optional[typing.Union[T, str]] = None, **kwargs):
126121
"""register a class or item to the registry
127122
example:
128123
@@ -158,7 +153,7 @@ def postgresql_connection:
158153
if item_or_custom_slug is not None and kwargs == {} and not isinstance(item_or_custom_slug, str):
159154
# register function is not called,
160155
# and item_or_custom_slug is a item (function or class).
161-
return self.__register()(item_or_custom_slug)
156+
return self.__register()(typing.cast(T, item_or_custom_slug))
162157

163158
if item_or_custom_slug is None and kwargs == {}:
164159
# unnecessary call
@@ -168,7 +163,7 @@ def postgresql_connection:
168163
)
169164

170165
# item_or_custom_slug is a custom_slug
171-
return self.__register(item_or_custom_slug, **kwargs)
166+
return self.__register(typing.cast(str, item_or_custom_slug), **kwargs)
172167

173168
def __repr__(self) -> str:
174169
parent = f"{self.parent_class.__name__}" if self.parent_class else ""

registerer/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RegistryValidator:
1010
1111
"""
1212

13-
def __init__(self, validator, *, error: str = None) -> None:
13+
def __init__(self, validator, *, error: typing.Optional[str] = None) -> None:
1414
self.validator = validator
1515
self.error = error
1616

tests/test_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Foo:
2222
with self.assertRaises(RegistrationError):
2323

2424
@registry.register("no")
25-
class Foo:
25+
class Foo2:
2626
pass
2727

2828

0 commit comments

Comments
 (0)