Skip to content

Commit 20bcea0

Browse files
chore(internal): codegen related update
1 parent d2d29bc commit 20bcea0

File tree

5 files changed

+19
-43
lines changed

5 files changed

+19
-43
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<!-- prettier-ignore -->
44
[![PyPI version](https://img.shields.io/pypi/v/cas-parser-python.svg?label=pypi%20(stable))](https://pypi.org/project/cas-parser-python/)
55

6-
The Cas Parser Python library provides convenient access to the Cas Parser REST API from any Python 3.8+
6+
The Cas Parser Python library provides convenient access to the Cas Parser REST API from any Python 3.9+
77
application. The library includes type definitions for all request params and response fields,
88
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
99

@@ -380,7 +380,7 @@ print(cas_parser.__version__)
380380

381381
## Requirements
382382

383-
Python 3.8 or higher.
383+
Python 3.9 or higher.
384384

385385
## Contributing
386386

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ dependencies = [
1515
"distro>=1.7.0, <2",
1616
"sniffio",
1717
]
18-
requires-python = ">= 3.8"
18+
requires-python = ">= 3.9"
1919
classifiers = [
2020
"Typing :: Typed",
2121
"Intended Audience :: Developers",
22-
"Programming Language :: Python :: 3.8",
2322
"Programming Language :: Python :: 3.9",
2423
"Programming Language :: Python :: 3.10",
2524
"Programming Language :: Python :: 3.11",
@@ -141,7 +140,7 @@ filterwarnings = [
141140
# there are a couple of flags that are still disabled by
142141
# default in strict mode as they are experimental and niche.
143142
typeCheckingMode = "strict"
144-
pythonVersion = "3.8"
143+
pythonVersion = "3.9"
145144

146145
exclude = [
147146
"_dev",

src/cas_parser/_models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import inspect
5+
import weakref
56
from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, Optional, cast
67
from datetime import date, datetime
78
from typing_extensions import (
@@ -573,6 +574,9 @@ class CachedDiscriminatorType(Protocol):
573574
__discriminator__: DiscriminatorDetails
574575

575576

577+
DISCRIMINATOR_CACHE: weakref.WeakKeyDictionary[type, DiscriminatorDetails] = weakref.WeakKeyDictionary()
578+
579+
576580
class DiscriminatorDetails:
577581
field_name: str
578582
"""The name of the discriminator field in the variant class, e.g.
@@ -615,8 +619,9 @@ def __init__(
615619

616620

617621
def _build_discriminated_union_meta(*, union: type, meta_annotations: tuple[Any, ...]) -> DiscriminatorDetails | None:
618-
if isinstance(union, CachedDiscriminatorType):
619-
return union.__discriminator__
622+
cached = DISCRIMINATOR_CACHE.get(union)
623+
if cached is not None:
624+
return cached
620625

621626
discriminator_field_name: str | None = None
622627

@@ -669,7 +674,7 @@ def _build_discriminated_union_meta(*, union: type, meta_annotations: tuple[Any,
669674
discriminator_field=discriminator_field_name,
670675
discriminator_alias=discriminator_alias,
671676
)
672-
cast(CachedDiscriminatorType, union).__discriminator__ = details
677+
DISCRIMINATOR_CACHE.setdefault(union, details)
673678
return details
674679

675680

src/cas_parser/_utils/_sync.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from __future__ import annotations
22

3-
import sys
43
import asyncio
54
import functools
6-
import contextvars
7-
from typing import Any, TypeVar, Callable, Awaitable
5+
from typing import TypeVar, Callable, Awaitable
86
from typing_extensions import ParamSpec
97

108
import anyio
@@ -15,34 +13,11 @@
1513
T_ParamSpec = ParamSpec("T_ParamSpec")
1614

1715

18-
if sys.version_info >= (3, 9):
19-
_asyncio_to_thread = asyncio.to_thread
20-
else:
21-
# backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread
22-
# for Python 3.8 support
23-
async def _asyncio_to_thread(
24-
func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
25-
) -> Any:
26-
"""Asynchronously run function *func* in a separate thread.
27-
28-
Any *args and **kwargs supplied for this function are directly passed
29-
to *func*. Also, the current :class:`contextvars.Context` is propagated,
30-
allowing context variables from the main thread to be accessed in the
31-
separate thread.
32-
33-
Returns a coroutine that can be awaited to get the eventual result of *func*.
34-
"""
35-
loop = asyncio.events.get_running_loop()
36-
ctx = contextvars.copy_context()
37-
func_call = functools.partial(ctx.run, func, *args, **kwargs)
38-
return await loop.run_in_executor(None, func_call)
39-
40-
4116
async def to_thread(
4217
func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
4318
) -> T_Retval:
4419
if sniffio.current_async_library() == "asyncio":
45-
return await _asyncio_to_thread(func, *args, **kwargs)
20+
return await asyncio.to_thread(func, *args, **kwargs)
4621

4722
return await anyio.to_thread.run_sync(
4823
functools.partial(func, *args, **kwargs),
@@ -53,10 +28,7 @@ async def to_thread(
5328
def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:
5429
"""
5530
Take a blocking function and create an async one that receives the same
56-
positional and keyword arguments. For python version 3.9 and above, it uses
57-
asyncio.to_thread to run the function in a separate thread. For python version
58-
3.8, it uses locally defined copy of the asyncio.to_thread function which was
59-
introduced in python 3.9.
31+
positional and keyword arguments.
6032
6133
Usage:
6234

tests/test_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from cas_parser._utils import PropertyInfo
1111
from cas_parser._compat import PYDANTIC_V1, parse_obj, model_dump, model_json
12-
from cas_parser._models import BaseModel, construct_type
12+
from cas_parser._models import DISCRIMINATOR_CACHE, BaseModel, construct_type
1313

1414

1515
class BasicModel(BaseModel):
@@ -809,7 +809,7 @@ class B(BaseModel):
809809

810810
UnionType = cast(Any, Union[A, B])
811811

812-
assert not hasattr(UnionType, "__discriminator__")
812+
assert not DISCRIMINATOR_CACHE.get(UnionType)
813813

814814
m = construct_type(
815815
value={"type": "b", "data": "foo"}, type_=cast(Any, Annotated[UnionType, PropertyInfo(discriminator="type")])
@@ -818,7 +818,7 @@ class B(BaseModel):
818818
assert m.type == "b"
819819
assert m.data == "foo" # type: ignore[comparison-overlap]
820820

821-
discriminator = UnionType.__discriminator__
821+
discriminator = DISCRIMINATOR_CACHE.get(UnionType)
822822
assert discriminator is not None
823823

824824
m = construct_type(
@@ -830,7 +830,7 @@ class B(BaseModel):
830830

831831
# if the discriminator details object stays the same between invocations then
832832
# we hit the cache
833-
assert UnionType.__discriminator__ is discriminator
833+
assert DISCRIMINATOR_CACHE.get(UnionType) is discriminator
834834

835835

836836
@pytest.mark.skipif(PYDANTIC_V1, reason="TypeAliasType is not supported in Pydantic v1")

0 commit comments

Comments
 (0)