Skip to content

Commit 260784c

Browse files
authored
Merge pull request #50 from pyiron/mypy
Add mypy
2 parents 6c47c6c + 3c35d3f commit 260784c

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

.github/workflows/push-pull.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
do-codacy: false
2323
do-coveralls: false
2424
do-ruff-check: true
25-
25+
do-mypy: true

pyiron_snippets/factory.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class creation via :func:`builtins.type` -- i.e. taking a class name, a tuple of
3333
from __future__ import annotations
3434

3535
from abc import ABCMeta
36+
from collections.abc import Callable
3637
from functools import wraps
3738
from importlib import import_module
3839
from inspect import Parameter, signature
@@ -57,7 +58,7 @@ class _FactoryTown(metaclass=_SingleInstance):
5758
factory object.
5859
"""
5960

60-
factories = {}
61+
factories: dict[str, _ClassFactory] = {}
6162

6263
@classmethod
6364
def clear(cls):
@@ -69,10 +70,12 @@ def clear(cls):
6970
cls.factories = {}
7071

7172
@staticmethod
72-
def _factory_address(factory_function: callable) -> str:
73+
def _factory_address(factory_function: Callable) -> str:
7374
return f"{factory_function.__module__}.{factory_function.__qualname__}"
7475

75-
def get_factory(self, factory_function: callable[..., type]) -> _ClassFactory:
76+
def get_factory(
77+
self, factory_function: Callable[..., tuple[str, tuple[type, ...], dict, dict]]
78+
) -> _ClassFactory:
7679

7780
self._verify_function_only_takes_positional_args(factory_function)
7881

@@ -103,7 +106,7 @@ def _build_factory(factory_function):
103106
return wraps(factory_function)(new_factory_class())
104107

105108
@staticmethod
106-
def _verify_function_only_takes_positional_args(factory_function: callable):
109+
def _verify_function_only_takes_positional_args(factory_function: Callable):
107110
parameters = signature(factory_function).parameters.values()
108111
if any(
109112
p.kind not in [Parameter.POSITIONAL_ONLY, Parameter.VAR_POSITIONAL]
@@ -136,7 +139,9 @@ class _ClassFactory(metaclass=_SingleInstance):
136139
For making dynamically created classes the same class.
137140
"""
138141

139-
_decorated_as_classfactory: ClassVar[bool] = False
142+
_decorated_as_classfactory: bool = False
143+
factory_function: ClassVar[Callable[..., tuple[str, tuple[type, ...], dict, dict]]]
144+
class_registry: ClassVar[dict[str, type[_FactoryMade]]] = {}
140145

141146
def __init_subclass__(cls, /, factory_function, **kwargs):
142147
super().__init_subclass__(**kwargs)
@@ -193,7 +198,7 @@ def _build_class(
193198
if "__module__" not in class_dict:
194199
class_dict["__module__"] = self.factory_function.__module__
195200
if "__qualname__" not in class_dict:
196-
class_dict["__qualname__"] = f"{self.__qualname__}.{name}"
201+
class_dict["__qualname__"] = f"{self.factory_function.__qualname__}.{name}"
197202
sc_init_kwargs["class_factory"] = self
198203
sc_init_kwargs["class_factory_args"] = class_factory_args
199204

@@ -248,7 +253,7 @@ class _FactoryMade:
248253
"""
249254

250255
# DEPRECATED: Use _reduce_imports_as instead
251-
_class_returns_from_decorated_function: ClassVar[callable | None] = None
256+
_class_returns_from_decorated_function: ClassVar[Callable | None] = None
252257

253258
_reduce_imports_as: ClassVar[tuple[str, str] | None] = None # Module and qualname
254259

@@ -337,7 +342,7 @@ def _instantiate_from_decorated(module, qualname, newargs_ex):
337342

338343

339344
def classfactory(
340-
factory_function: callable[..., tuple[str, tuple[type, ...], dict, dict]],
345+
factory_function: Callable[..., tuple[str, tuple[type, ...], dict, dict]],
341346
) -> _ClassFactory:
342347
"""
343348
A decorator for building dynamic class factories whose classes are unique and whose
@@ -354,7 +359,7 @@ def classfactory(
354359
be overridden.
355360
356361
Args:
357-
factory_function (callable[..., tuple[str, tuple[type, ...], dict, dict]]):
362+
factory_function (Callable[..., tuple[str, tuple[type, ...], dict, dict]]):
358363
A function returning arguments that would be passed to `builtins.type` to
359364
dynamically generate a class. The function must accept exclusively
360365
positional arguments

pyiron_snippets/retry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import warnings
55
from collections.abc import Callable
66
from itertools import count
7-
from typing import TypeVar
7+
from typing import Any, TypeVar
88

99
T = TypeVar("T")
1010

@@ -16,7 +16,7 @@ def retry(
1616
at_most: int | None = None,
1717
delay: float = 1.0,
1818
delay_factor: float = 1.0,
19-
log: bool | object = True,
19+
log: bool | Any = True,
2020
) -> T:
2121
"""
2222
Try to call `func` until it no longer raises `error`.

pyiron_snippets/singleton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Singleton(ABCMeta):
1414
1515
"""
1616

17-
_instances = {}
17+
_instances: dict[type, object] = {}
1818

1919
def __call__(cls, *args, **kwargs):
2020
if cls not in cls._instances:

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ extend-exclude = '''
8585
^/docs
8686
)
8787
'''
88+
89+
[tool.mypy]
90+
exclude = [
91+
"^docs/conf\\.py$",
92+
"^pyiron_snippets/files\\.py$",
93+
"^tests/",
94+
]
95+
ignore_missing_imports = true
96+
strict_equality = true
97+
non_interactive = true
98+
install_types = true
99+

0 commit comments

Comments
 (0)