Skip to content

Commit 2ff359e

Browse files
committed
Remove typing.cast
It's too easy to misunderstand that typing.cast is not a runtime thing. Much more obvious with a type ignore comment
1 parent 984f7bf commit 2ff359e

File tree

14 files changed

+59
-50
lines changed

14 files changed

+59
-50
lines changed

strcs/annotations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
object as a creator override.
2020
"""
2121

22-
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar, cast, runtime_checkable
22+
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar, runtime_checkable
2323

2424
import attrs
2525

@@ -305,7 +305,7 @@ def adjusted_creator(
305305
type_cache: TypeCache,
306306
) -> ConvertFunction[T] | None:
307307
def retrieve(value: object, /, _meta: Meta) -> ConvertResponse[T]:
308-
return cast(T, _meta.retrieve_one(object, "retrieved", type_cache=type_cache))
308+
return _meta.retrieve_one(object, "retrieved", type_cache=type_cache) # type: ignore[return-value]
309309

310310
a = Ann[T](creator=retrieve)
311311
return a.adjusted_creator(creator, register, typ, type_cache)

strcs/decorator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def create_thing(value: object, /) -> strcs.ConvertResponse[Thing]:
7272

7373
import inspect
7474
from collections.abc import Callable, Generator, Mapping
75-
from typing import TYPE_CHECKING, Generic, TypeVar, cast
75+
from typing import TYPE_CHECKING, Generic, TypeVar
7676

7777
import attrs
7878
import cattrs
@@ -197,7 +197,7 @@ def __call__(self, create_args: "CreateArgs") -> T:
197197

198198
if self.assume_unchanged_converted and want.is_type_for(value):
199199
if want.origin_type not in builtin_types:
200-
return cast(T, value)
200+
return value # type: ignore[return-value]
201201

202202
try:
203203
args = ArgsExtractor(
@@ -260,7 +260,7 @@ def deal(res: ConvertResponse[T], value: object) -> T:
260260
reason="Told to use NotSpecified as the final value",
261261
creator=self.func,
262262
)
263-
return cast(T, value)
263+
return value # type: ignore[return-value]
264264
else:
265265
if not isinstance(res, Mapping) and issubclass(
266266
want.checkable, self.type_cache.disassemble(type(res)).checkable

strcs/disassemble/_base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
TypeGuard,
1515
TypeVar,
1616
Union,
17-
cast,
1817
overload,
1918
)
2019

@@ -139,7 +138,7 @@ def create(
139138
original = typ
140139

141140
if isinstance(typ, cls):
142-
return cast(Type[U], typ)
141+
return typ # type: ignore[return-value]
143142

144143
if typ in cache:
145144
return cache[original]
@@ -160,7 +159,7 @@ def create(
160159
type_alias = extracted
161160
extracted = extracted.__supertype__
162161

163-
constructor = cast(Callable[..., Type[U]], cls)
162+
constructor: Callable[..., Type[U]] = cls # type: ignore[assignment]
164163

165164
made = constructor(
166165
cache=cache,
@@ -723,7 +722,7 @@ def checkable_as_type(self) -> type[T]:
723722
Return ``self.checkable``, but the return type of this function is a
724723
python type of the inner type represented by this :class:`strcs.Type`
725724
"""
726-
return cast(type[T], self.checkable)
725+
return self.checkable # type: ignore[return-value]
727726

728727
@memoized_property
729728
def checkable(self) -> type[InstanceCheck]:

strcs/disassemble/_comparer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import functools
22
import operator
33
import typing
4-
from typing import TYPE_CHECKING, Annotated, NewType, Optional, cast
4+
from typing import TYPE_CHECKING, Annotated, NewType, Optional
55

66
import attrs
77

@@ -27,7 +27,7 @@ class Distilled:
2727
def classinfo(self) -> type | tuple[type, ...]:
2828
if not self.is_valid:
2929
raise NotValidType()
30-
return cast(type | tuple[type, ...], self.original)
30+
return self.original # type: ignore[return-value]
3131

3232
@property
3333
def as_tuple(self) -> tuple[type, ...]:

strcs/disassemble/_creation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Mapping, MutableMapping
2-
from typing import TypeVar, cast
2+
from typing import TypeVar
33

44
import attrs
55
import cattrs
@@ -49,7 +49,7 @@ def instantiate(want: Type[T], res: object, converter: cattrs.Converter) -> T:
4949
"""
5050
if res is None:
5151
if want.optional or want.original is None:
52-
return cast(T, None)
52+
return None # type: ignore[return-value]
5353

5454
raise ValueError("Can't instantiate object with None")
5555

@@ -70,7 +70,7 @@ def instantiate(want: Type[T], res: object, converter: cattrs.Converter) -> T:
7070
continue
7171

7272
val = res[name]
73-
attribute = cast(attrs.Attribute, field)
73+
attribute: attrs.Attribute = field # type: ignore[assignment]
7474
conv_obj[name] = converter._structure_attribute(attribute, val)
7575

7676
return instantiator(**conv_obj)

strcs/hints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
TypeGuard,
2424
TypeVar,
2525
Union,
26-
cast,
2726
runtime_checkable,
2827
)
2928

@@ -302,7 +301,8 @@ class Other:
302301
else:
303302
return cls
304303

305-
cast(WithResolvedTypes[C], cls).__strcs_types_resolved__ = cls
304+
with_resolved_types: WithResolvedTypes[C] = cls # type: ignore[assignment]
305+
with_resolved_types.__strcs_types_resolved__ = cls
306306

307307
# Copied from standard library typing.get_type_hints
308308
# Cause I need globals/locals to resolve nested types that don't have forwardrefs

strcs/hooks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
for specific properties on a class.
88
"""
99

10-
from typing import TYPE_CHECKING, TypeVar, cast
10+
from typing import TYPE_CHECKING, TypeVar
1111

1212
import cattrs
1313

@@ -208,4 +208,7 @@ def bypass(self, value: object, want: Type[T]) -> T:
208208
self.converter._structure_func.dispatch.cache_clear()
209209
if isinstance(value, dict):
210210
value = fill(want, value)
211-
return self.converter.structure(value, cast(type, want.original))
211+
return self.converter.structure(
212+
value,
213+
want.original, # type: ignore[arg-type]
214+
)

strcs/memoized_property.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, MutableMapping
2-
from typing import Generic, TypeVar, Union, cast, overload
2+
from typing import Generic, TypeVar, Union, overload
33

44
PropRet = TypeVar("PropRet")
55

@@ -66,7 +66,7 @@ def __get__(
6666
if self.name not in cache:
6767
cache[self.name] = self.func(instance)
6868

69-
return cast(PropRet, cache[self.name])
69+
return cache[self.name] # type: ignore[return-value]
7070

7171
def __delete__(self, instance: object) -> None:
7272
cache = self.cache(instance)

strcs/meta.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fnmatch
22
import inspect
33
from collections.abc import Iterable, Mapping
4-
from typing import Protocol, TypeVar, cast, overload
4+
from typing import Protocol, TypeVar, overload
55

66
import attrs
77
import cattrs
@@ -117,7 +117,7 @@ def narrow(self, *patterns: str, obj: Mapping | object = Empty) -> dict[str, obj
117117
pattern = pattern[1:]
118118
for n in self.keys_from(obj):
119119
if progress.obj_is_mapping:
120-
v = cast(Mapping, obj)[n]
120+
v = obj[n] # type: ignore[index]
121121
else:
122122
v = getattr(obj, n)
123123

@@ -202,7 +202,7 @@ def find_by_type(
202202
if data is Empty:
203203
data = self.data
204204

205-
data = cast(dict[str, object], data)
205+
data: dict[str, object] = data # type: ignore[assignment]
206206

207207
if typ is object:
208208
return False, data
@@ -288,15 +288,15 @@ def retrieve_one(
288288
if with_patterns or typ is object:
289289
data = with_patterns
290290
elif default is not inspect._empty:
291-
return cast(T, default)
291+
return default # type: ignore[return-value]
292292

293293
optional, found = self.find_by_type(typ, data=data, type_cache=type_cache)
294294

295295
if patterns and data and not found and not optional:
296296
raise errors.FoundWithWrongType(patterns=list(patterns), want=typ)
297297

298298
if optional and not found:
299-
return cast(T, None)
299+
return None # type: ignore[return-value]
300300

301301
if len(found) == 1:
302302
for thing in found.values():
@@ -306,7 +306,7 @@ def retrieve_one(
306306
raise errors.MultipleNamesForType(want=typ, found=sorted(found))
307307

308308
if default is not inspect._empty:
309-
return cast(T, default)
309+
return default # type: ignore[return-value]
310310

311311
raise errors.NoDataByTypeName(
312312
want=typ,

strcs/register.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
specific types.
44
"""
55

6-
from typing import Annotated, ClassVar, Generic, Protocol, TypeVar, cast
6+
from typing import Annotated, ClassVar, Generic, Protocol, TypeVar
77

88
import cattrs
99

@@ -173,12 +173,12 @@ def __call__(
173173
typ = self.original
174174

175175
self.wrapped = WrappedCreator[T](
176-
cast(Type[T], typ),
176+
typ, # type: ignore[arg-type]
177177
func,
178178
type_cache=register.type_cache,
179179
assume_unchanged_converted=self.assume_unchanged_converted,
180180
)
181-
self.typ = cast(Type[T], typ)
181+
self.typ = typ # type: ignore[assignment]
182182
self.func = self.wrapped.func
183183

184184
register[typ] = self.wrapped
@@ -238,10 +238,11 @@ def create_annotated(
238238
This is the same as ``reg.create`` but the type will be wrapped with the
239239
provided annotation.
240240
"""
241+
want: Type[T]
241242
if isinstance(typ, Type):
242243
want = typ
243244
else:
244-
want = cast(Type[T], self.type_cache.disassemble(Annotated[typ, ann]))
245+
want = self.type_cache.disassemble(Annotated[typ, ann]) # type: ignore[assignment]
245246

246247
return CreateStructureHook.structure(
247248
register=self,

0 commit comments

Comments
 (0)