Skip to content

Commit e449ad2

Browse files
committed
Import from typing rather than use tp import alias
1 parent f813085 commit e449ad2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+819
-805
lines changed

README.rst

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

1717
.. code-block:: python
1818
19-
import typing as tp
19+
from typing import Annotated, NewType
2020
2121
import attrs
2222
@@ -25,8 +25,8 @@ Example
2525
reg = strcs.CreateRegister()
2626
creator = reg.make_decorator()
2727
28-
Number = tp.NewType("Number", int)
29-
Word = tp.NewType("Word", str)
28+
Number = NewType("Number", int)
29+
Word = NewType("Word", str)
3030
3131
3232
@attrs.define(frozen=True)
@@ -43,9 +43,9 @@ Example
4343
4444
@attrs.define
4545
class Config:
46-
thing: tp.Annotated[Thing, strcs.FromMeta("thing")]
46+
thing: Annotated[Thing, strcs.FromMeta("thing")]
4747
words: list[Word]
48-
some_number: tp.Annotated[Number, Maths(multiply=2)]
48+
some_number: Annotated[Number, Maths(multiply=2)]
4949
contrived1: str
5050
contrived2: str
5151
some_other_number: int = 16

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Here is a contrived example that shows a couple features. Read the
6565
from my_library import Renderer
6666
6767
from attrs import define
68-
import typing as tp
68+
from typing import Annotated
6969
import strcs
7070
7171
@@ -82,7 +82,7 @@ Here is a contrived example that shows a couple features. Read the
8282
8383
# Here we're using dependency injection to say get ``renderer`` from
8484
# the meta object we provide when creating objects.
85-
renderer: tp.Annotated[Renderer, strcs.FromMeta("renderer")]
85+
renderer: Annotated[Renderer, strcs.FromMeta("renderer")]
8686
8787
8888
@define

docs/strcs/features/annotations.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Annotations
88
.. code-block:: python
99
1010
import attrs
11-
import typing as tp
11+
from typing import Annotated
1212
import strcs
1313
1414
reg = strcs.CreateRegister()
@@ -27,14 +27,14 @@ Annotations
2727
2828
@attrs.define
2929
class Thing:
30-
val: tp.Annotated[int, strcs.Ann(MathsAnnotation(addition=20), do_maths)]
30+
val: Annotated[int, strcs.Ann(MathsAnnotation(addition=20), do_maths)]
3131
3232
3333
@attrs.define
3434
class Holder:
3535
once: Thing
36-
twice: tp.Annotated[Thing, MathsAnnotation(multiplication=2)]
37-
thrice: tp.Annotated[Thing, MathsAnnotation(multiplication=3)]
36+
twice: Annotated[Thing, MathsAnnotation(multiplication=2)]
37+
thrice: Annotated[Thing, MathsAnnotation(multiplication=3)]
3838
3939
4040
@creator(Thing)

docs/strcs/features/creators.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ this by returning a dictionary that cattrs will then use to create the instance.
4545

4646
.. code-block:: python
4747
48-
import typing as tp
4948
import strcs
5049
5150
@@ -189,7 +188,7 @@ argument in the creator so that an infinite loop may be avoided.
189188
.. code-block:: python
190189
191190
import attrs
192-
import typing as tp
191+
from typing import Annotated
193192
import secrets
194193
import strcs
195194
@@ -201,7 +200,7 @@ argument in the creator so that an infinite loop may be avoided.
201200
@attrs.define
202201
class Part:
203202
one: int
204-
identity: tp.Annotated[str, strcs.FromMeta("identity")]
203+
identity: Annotated[str, strcs.FromMeta("identity")]
205204
206205
207206
@attrs.define
@@ -298,7 +297,7 @@ Generator creators may also yield other generators:
298297
.. code-block:: python
299298
300299
import attrs
301-
import typing as tp
300+
from collections.abc import Generator
302301
import strcs
303302
304303
reg = strcs.CreateRegister()
@@ -317,7 +316,7 @@ Generator creators may also yield other generators:
317316
self.three = None
318317
319318
320-
def recursion_is_fun(value: object) -> tp.Generator[dict, Thing, None]:
319+
def recursion_is_fun(value: object) -> Generator[dict, Thing, None]:
321320
assert isinstance(value, dict)
322321
assert value == {"one": 20}
323322
called.append(2)
@@ -327,7 +326,7 @@ Generator creators may also yield other generators:
327326
328327
329328
@creator(Thing)
330-
def make(value: object) -> tp.Generator[tp.Generator[dict, Thing, None], Thing, None]:
329+
def make(value: object) -> Generator[Generator[dict, Thing, None], Thing, None]:
331330
called.append(1)
332331
made = yield recursion_is_fun(value)
333332
made.three = 222

strcs/annotations.py

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

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

2424
import attrs
2525

@@ -32,14 +32,14 @@
3232
from .disassemble import Type, TypeCache
3333
from .meta import Meta
3434

35-
if tp.TYPE_CHECKING:
35+
if TYPE_CHECKING:
3636
from .register import CreateRegister
3737

38-
T = tp.TypeVar("T")
38+
T = TypeVar("T")
3939

4040

41-
@tp.runtime_checkable
42-
class AdjustableMeta(tp.Protocol[T]):
41+
@runtime_checkable
42+
class AdjustableMeta(Protocol[T]):
4343
"""
4444
An interface used to modify the meta object when creating a field.
4545
@@ -55,8 +55,8 @@ class AdjustableMeta(tp.Protocol[T]):
5555
def adjusted_meta(self, meta: Meta, typ: Type[T], type_cache: TypeCache) -> Meta: ...
5656

5757

58-
@tp.runtime_checkable
59-
class AdjustableCreator(tp.Protocol[T]):
58+
@runtime_checkable
59+
class AdjustableCreator(Protocol[T]):
6060
"""
6161
An interface used to modify the creator used when creating a field.
6262
@@ -105,7 +105,7 @@ class MetaAnnotation:
105105
.. code-block:: python
106106
107107
import attrs
108-
import typing as tp
108+
from typing import Annotated
109109
import strcs
110110
111111
reg = strcs.CreateRegister()
@@ -120,7 +120,7 @@ class MyAnnotation(strcs.MetaAnnotation):
120120
121121
@attrs.define
122122
class MyKls:
123-
key: tp.Annotated[str, MyAnnotation(one=1, two=2)]
123+
key: Annotated[str, MyAnnotation(one=1, two=2)]
124124
125125
126126
@creator(MyKls)
@@ -147,7 +147,7 @@ class MergedMetaAnnotation:
147147
.. code-block:: python
148148
149149
import attrs
150-
import typing as tp
150+
from typing import Annotated
151151
import strcs
152152
153153
reg = strcs.CreateRegister()
@@ -162,7 +162,7 @@ class MyAnnotation(strcs.MergedMetaAnnotation):
162162
163163
@attrs.define
164164
class MyKls:
165-
key: tp.Annotated[str, MyAnnotation(one=1, two=2)]
165+
key: Annotated[str, MyAnnotation(one=1, two=2)]
166166
167167
168168
@@ -193,7 +193,7 @@ def create_mykls(value: object, /, one: int = 0, two: int = 0) -> dict | None:
193193
"""
194194

195195

196-
class Ann(tp.Generic[T]):
196+
class Ann(Generic[T]):
197197
"""
198198
A concrete implementation of both ``strcs.AdjustedMeta`` and
199199
``strcs.AdjustedCreator``.
@@ -270,7 +270,7 @@ class FromMeta:
270270
.. code-block:: python
271271
272272
import attrs
273-
import typing as tp
273+
from typing import Annotated
274274
import strcs
275275
276276
reg = strcs.CreateRegister()
@@ -284,7 +284,7 @@ def incantation(self) -> str:
284284
285285
@attrs.define
286286
class Wizard:
287-
magic: tp.Annotated[Magic, strcs.FromMeta("magic")]
287+
magic: Annotated[Magic, strcs.FromMeta("magic")]
288288
289289
290290
wizard = reg.create(Wizard, meta=reg.meta({"magic": Magic()}))
@@ -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 tp.cast(T, _meta.retrieve_one(object, "retrieved", type_cache=type_cache))
308+
return cast(T, _meta.retrieve_one(object, "retrieved", type_cache=type_cache))
309309

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

strcs/args_extractor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
"""
1111

1212
import inspect
13-
import typing as tp
13+
from typing import TYPE_CHECKING, Generic, TypeVar
1414

1515
import cattrs
1616

1717
from .disassemble import Type
1818
from .meta import Meta
1919

20-
if tp.TYPE_CHECKING:
20+
if TYPE_CHECKING:
2121
from .decorator import ConvertDefinition
2222
from .register import CreateRegister
2323

24-
T = tp.TypeVar("T")
24+
T = TypeVar("T")
2525

2626

27-
class ArgsExtractor(tp.Generic[T]):
27+
class ArgsExtractor(Generic[T]):
2828
def __init__(
2929
self,
3030
*,

strcs/decorator.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def create_thing(value: object, /) -> strcs.ConvertResponse[Thing]:
7171
"""
7272

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

7777
import attrs
7878
import cattrs
@@ -84,14 +84,14 @@ def create_thing(value: object, /) -> strcs.ConvertResponse[Thing]:
8484
from .not_specified import NotSpecified, NotSpecifiedMeta
8585
from .standard import builtin_types
8686

87-
if tp.TYPE_CHECKING:
87+
if TYPE_CHECKING:
8888
from .register import CreateRegister
8989

90-
T = tp.TypeVar("T")
90+
T = TypeVar("T")
9191

9292

9393
@attrs.define
94-
class CreateArgs(tp.Generic[T]):
94+
class CreateArgs(Generic[T]):
9595
"""
9696
The object given to ``ConvertFunction`` objects to produce an instance of the
9797
desired type.
@@ -108,7 +108,7 @@ class CreateArgs(tp.Generic[T]):
108108
# Either a value instructing strcs to do something, an object that strcs should
109109
# use as is, or a generator that can operate on the object strcs creates.
110110
type ConvertResponseValues[T] = bool | dict[str, object] | T | NotSpecifiedMeta
111-
type ConvertResponseGenerator[T] = tp.Generator[ConvertResponseValues[T] | tp.Generator | None, T]
111+
type ConvertResponseGenerator[T] = Generator[ConvertResponseValues[T] | Generator | None, T]
112112
type ConvertResponse[T] = ConvertResponseValues[T] | ConvertResponseGenerator[T] | None
113113

114114
# ConvertDefinition is the developer provided functions that do transformation
@@ -124,11 +124,11 @@ class CreateArgs(tp.Generic[T]):
124124
# - want: Type[T] = The strcs.Type object for the desired type
125125
# - meta values are from the meta object, or the special objects known by
126126
# strcs.ArgExtractor
127-
type ConvertDefinition[T] = tp.Callable[..., ConvertResponse[T]]
127+
type ConvertDefinition[T] = Callable[..., ConvertResponse[T]]
128128

129129
# ConvertFunction is the object the strcs.CreateRegister interacts with to invoke
130130
# the ConvertDefinition objects.
131-
type ConvertFunction[T] = tp.Callable[[CreateArgs[T]], T]
131+
type ConvertFunction[T] = Callable[[CreateArgs[T]], T]
132132

133133

134134
def take_or_make(value: object, want: Type[T], /) -> ConvertResponse[T]:
@@ -148,7 +148,7 @@ def take_or_make(value: object, want: Type[T], /) -> ConvertResponse[T]:
148148
return None
149149

150150

151-
class WrappedCreator(tp.Generic[T]):
151+
class WrappedCreator(Generic[T]):
152152
"""
153153
An implementation of ``strcs.ConvertFunction`` that operates on the provided
154154
ConvertDefinition.
@@ -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 tp.cast(T, value)
200+
return cast(T, 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 tp.cast(T, value)
263+
return cast(T, value)
264264
else:
265265
if not isinstance(res, Mapping) and issubclass(
266266
want.checkable, self.type_cache.disassemble(type(res)).checkable
@@ -288,7 +288,7 @@ def _process_generator(
288288
self,
289289
res: ConvertResponseGenerator[T],
290290
value: object,
291-
deal: tp.Callable[[ConvertResponse[T], object], T],
291+
deal: Callable[[ConvertResponse[T], object], T],
292292
) -> T:
293293
try:
294294
made: ConvertResponse[T]

0 commit comments

Comments
 (0)