Skip to content

Commit 897f329

Browse files
authored
fix: Apply strict typing. (#48)
1 parent 1b77777 commit 897f329

File tree

8 files changed

+34
-37
lines changed

8 files changed

+34
-37
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ classifiers = [
3838
"Intended Audience :: Developers",
3939
]
4040
name = "type-lens"
41-
version = "0.2.3"
41+
version = "0.2.4"
4242
description = "type-lens is a Python template project designed to simplify the setup of a new project."
4343
readme = "README.md"
4444
license = { text = "MIT" }
@@ -127,6 +127,7 @@ warn_unused_configs = true
127127
warn_unused_ignores = true
128128

129129
[tool.pyright]
130+
typeCheckingMode = "strict"
130131
disableBytesTypePromotions = true
131132
exclude = [
132133
"tools",

tests/test_callable_view.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def fn() -> int:
4444

4545
def test_untyped_param() -> None:
4646
def fn(foo): # type: ignore[no-untyped-def]
47-
return foo
47+
return foo # pyright: ignore
4848

49-
function_view = CallableView.from_callable(fn)
49+
function_view = CallableView.from_callable(fn) # pyright: ignore
5050
assert function_view.parameters == (ParameterView("foo", TypeView(Any), has_annotation=False),)
5151

5252

@@ -127,7 +127,7 @@ def method(self, c: bool) -> bool:
127127
def test_parameters_with_none_default(hint: Any) -> None:
128128
def fn(plain: hint = None, annotated: Annotated[hint, ...] = None) -> None: ... # pyright: ignore
129129

130-
fn_view = CallableView.from_callable(fn, localns=locals(), include_extras=True)
130+
fn_view = CallableView.from_callable(fn, localns=locals(), include_extras=True) # pyright: ignore
131131
plain_param, annotated_param = fn_view.parameters
132132
assert plain_param.type_view.annotation == annotated_param.type_view.annotation
133133

tests/test_type_view.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
T = TypeVar("T")
3232

3333

34-
def _check_parsed_type(type_lens: TypeView, expected: dict[str, Any]) -> None:
34+
def _check_parsed_type(type_lens: TypeView[Any], expected: dict[str, Any]) -> None:
3535
__tracebackhide__ = True
3636
for key, expected_value in expected.items():
3737
lens_value = getattr(type_lens, key)
@@ -334,8 +334,8 @@ def test_tuple() -> None:
334334
assert TypeView(Tuple[int, ...]).is_tuple is True
335335
assert TypeView(Tuple[int, ...]).is_variadic_tuple is True
336336

337-
assert TypeView(...).is_tuple is False
338-
assert TypeView(...).is_variadic_tuple is False
337+
assert TypeView(...).is_tuple is False # pyright: ignore
338+
assert TypeView(...).is_variadic_tuple is False # pyright: ignore
339339

340340

341341
def test_strip_optional() -> None:

type_lens/callable_view.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class CallableView:
19-
def __init__(self, fn: Callable, type_hints: dict[str, type]):
19+
def __init__(self, fn: Callable[..., Any], type_hints: dict[str, type]):
2020
self.callable = fn
2121
self.signature = getattr(fn, "__signature__", None) or inspect.signature(fn)
2222

@@ -45,7 +45,7 @@ def __repr__(self) -> str:
4545
@classmethod
4646
def from_callable(
4747
cls: type[Self],
48-
fn: Callable,
48+
fn: Callable[..., Any],
4949
*,
5050
globalns: dict[str, Any] | None = None,
5151
localns: dict[str, Any] | None = None,
@@ -61,17 +61,3 @@ def from_callable(
6161

6262
result = get_type_hints(hint_fn, globalns=globalns, localns=localns, include_extras=include_extras)
6363
return cls(fn, result)
64-
65-
66-
def _fix_annotated_optional_type_hints(
67-
hints: dict[str, Any],
68-
) -> dict[str, Any]: # pragma: no cover
69-
"""Normalize `Annotated` interacting with `get_type_hints` in versions <3.11.
70-
71-
https://github.com/python/cpython/issues/90353.
72-
"""
73-
for param_name, hint in hints.items():
74-
type_view = TypeView(hint)
75-
if type_view.is_union and type_view.inner_types[0].is_annotated:
76-
hints[param_name] = type_view.inner_types[0].raw
77-
return hints

type_lens/parameter_view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ParameterView:
3232
def __init__(
3333
self,
3434
name: str,
35-
type_view: TypeView = _any_type_view,
35+
type_view: TypeView[Any] = _any_type_view,
3636
*,
3737
default: Any | EmptyType = Empty,
3838
has_annotation: bool = True,

type_lens/type_view.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
from collections import abc
44
from collections.abc import Collection, Mapping
5-
from typing import Any, AnyStr, Final, ForwardRef, Generic, Literal, TypeVar, Union, _SpecialForm
5+
from typing import (
6+
Any,
7+
AnyStr,
8+
Final,
9+
ForwardRef,
10+
Generic,
11+
Literal,
12+
TypeVar,
13+
Union,
14+
_SpecialForm, # pyright: ignore[reportPrivateUsage]
15+
)
616

717
from typing_extensions import Annotated, NotRequired, Required, get_args, get_origin
818
from typing_extensions import Literal as ExtensionsLiteral
@@ -44,12 +54,12 @@ def __init__(self, annotation: T) -> None:
4454
unwrapped, metadata, wrappers = unwrap_annotation(annotation)
4555
origin = get_origin(unwrapped)
4656

47-
args: tuple[Any, ...] = () if origin is abc.Callable else get_args(unwrapped)
57+
args: tuple[Any, ...] = () if origin is abc.Callable else get_args(unwrapped) # pyright: ignore
4858

4959
self.raw: Final[T] = annotation
5060
self.annotation: Final = unwrapped
51-
self.origin: Final = origin
52-
self.fallback_origin: Final = origin or unwrapped
61+
self.origin: Final[Any] = origin
62+
self.fallback_origin: Final[Any] = origin or unwrapped
5363
self.args: Final[tuple[Any, ...]] = args
5464
self.metadata: Final = metadata
5565
self._wrappers: Final = wrappers
@@ -79,23 +89,23 @@ def repr_type(self) -> str:
7989
"""
8090
# Literal/Union both appear to have no name on some versions of python.
8191
if self.is_literal:
82-
name = "Literal"
92+
name: str = "Literal"
8393
elif self.is_union:
8494
name = "Union"
8595
elif isinstance(self.annotation, (type, _SpecialForm)) or self.origin:
8696
try:
87-
name = self.annotation.__name__ # pyright: ignore[reportAttributeAccessIssue]
97+
name = str(self.annotation.__name__) # pyright: ignore
8898
except AttributeError:
8999
# Certain _SpecialForm items have no __name__ python 3.8.
90-
name = self.annotation._name # pyright: ignore[reportAttributeAccessIssue]
100+
name = str(self.annotation._name) # pyright: ignore
91101
else:
92102
name = repr(self.annotation)
93103

94104
if self.origin:
95105
inner_types = ", ".join(t.repr_type for t in self.inner_types)
96106
name = f"{name}[{inner_types}]"
97107

98-
return str(name)
108+
return name
99109

100110
@property
101111
def allows_none(self) -> bool:
@@ -192,7 +202,7 @@ def is_variadic_tuple(self) -> bool:
192202
Tuples like `tuple[int, ...]` represent a list-like unbounded sequence
193203
of a single type T.
194204
"""
195-
return self.is_tuple and len(self.args) == 2 and self.args[1] == ...
205+
return self.is_tuple and len(self.args) == 2 and self.args[1] == ... # pyright: ignore
196206

197207
@property
198208
def safe_generic_origin(self) -> Any:
@@ -249,7 +259,7 @@ def is_subclass_of(self, typ: Any | tuple[Any, ...], /) -> bool:
249259
"""
250260
return isinstance(self.fallback_origin, type) and issubclass(self.fallback_origin, typ)
251261

252-
def strip_optional(self) -> TypeView:
262+
def strip_optional(self) -> TypeView[Any]:
253263
if not self.is_optional:
254264
return self
255265

type_lens/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def fix_annotated_optional_type_hints(
4444
_get_type_hints = typing.get_type_hints # pyright: ignore
4545

4646
else:
47-
from eval_type_backport import eval_type_backport
47+
from eval_type_backport import eval_type_backport # pyright: ignore
4848

4949
@typing.no_type_check
5050
def _get_type_hints( # noqa: C901

type_lens/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def unwrap_annotation(annotation: t.Any) -> tuple[t.Any, tuple[t.Any, ...], set[
9494
A tuple of the unwrapped annotation and any ``Annotated`` metadata, and a set of any wrapper types encountered.
9595
"""
9696
origin = te.get_origin(annotation)
97-
wrappers = set()
98-
metadata = []
97+
wrappers: set[t.Any] = set()
98+
metadata: list[t.Any] = []
9999
while origin in _WRAPPER_TYPES:
100100
wrappers.add(origin)
101101
annotation, *meta = te.get_args(annotation)

0 commit comments

Comments
 (0)