Skip to content

Commit fa72757

Browse files
committed
fix forward reference resolution and other issues
1 parent f535c48 commit fa72757

4 files changed

Lines changed: 54 additions & 32 deletions

File tree

src/prisma/generator/templates/_typing_imports.py.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ from builtins import float as _float
4040
{% if recursive_types %}
4141
from typing_extensions import LiteralString
4242
{% else %}
43-
LiteralString = str
43+
from typing import TypeAlias
44+
LiteralString: TypeAlias = str
4445
{% endif %}

src/prisma/generator/templates/types/__init__.py.jinja

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ from typing import TYPE_CHECKING, Any
99
{% if recursive_types %}
1010
from typing_extensions import LiteralString # noqa: F401
1111
{% else %}
12-
LiteralString = str
12+
from typing import TypeAlias
13+
14+
LiteralString: TypeAlias = str
1315
{% endif %}
1416

17+
# List of submodules for validator forward reference resolution
18+
_SUBMODULES = ['atomic', 'filters', 'list_filters'{% for model in dmmf.datamodel.models %}, '{{ model.name | lower }}'{% endfor %}]
19+
1520
if TYPE_CHECKING:
1621
# Direct re-exports for type checking
1722
from .atomic import * # noqa: F401, F403
@@ -21,16 +26,22 @@ if TYPE_CHECKING:
2126
from .{{ model.name | lower }} import * # noqa: F401, F403
2227
{% endfor %}
2328
else:
29+
import importlib
30+
2431
# Lazy attribute access at runtime
2532
def __getattr__(name: str) -> Any:
2633
"""Allow attribute access to types defined in submodules."""
2734
# Try each submodule
28-
import importlib
29-
for module_name in ['atomic', 'filters', 'list_filters'{% for model in dmmf.datamodel.models %}, '{{ model.name | lower }}'{% endfor %}]:
30-
try:
31-
module = importlib.import_module(f'.{module_name}', __package__)
32-
if hasattr(module, name):
33-
return getattr(module, name)
34-
except (ImportError, AttributeError):
35-
continue
35+
for module_name in _SUBMODULES:
36+
module = importlib.import_module(f'.{module_name}', __package__)
37+
if hasattr(module, name):
38+
return getattr(module, name)
3639
raise AttributeError(f"module 'prisma.types' has no attribute {name!r}")
40+
41+
def __dict__():
42+
return {
43+
name
44+
for module_name in _SUBMODULES
45+
for name in dir(importlib.import_module(f'.{module_name}', __package__))
46+
if not name.startswith('_')
47+
}

src/prisma/generator/templates/types/_model.py.jinja

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,6 @@ from .list_filters import *
1414

1515
# --- Types for model: {{ model.name }} --- #
1616

17-
{% set other_models = all_models | selectattr('name', 'ne', model.name) | list %}
18-
{% if other_models %}
19-
# Cross-import related model types for type checker resolution
20-
if TYPE_CHECKING:
21-
{% for other_model in other_models %}
22-
from .{{ other_model.name | lower }} import (
23-
{{ other_model.name }}WhereInput,
24-
{{ other_model.name }}WhereUniqueInput,
25-
{{ other_model.name }}OrderByInput,
26-
{{ other_model.name }}ScalarFieldKeys,
27-
{{ other_model.name }}RelationFilter,
28-
{{ other_model.name }}ListRelationFilter,
29-
{{ other_model.name }}CreateWithoutRelationsInput,
30-
{{ other_model.name }}CreateNestedWithoutRelationsInput,
31-
{{ other_model.name }}CreateManyNestedWithoutRelationsInput,
32-
{{ other_model.name }}UpdateOneWithoutRelationsInput,
33-
{{ other_model.name }}UpdateManyWithoutRelationsInput,
34-
{{ other_model.name }}ConnectOrCreateWithoutRelationsInput,
35-
)
36-
{% endfor %}
37-
{% endif %}
38-
3917

4018
{% set depth = generator.config.recursive_type_depth %}
4119

@@ -432,3 +410,24 @@ class {{ model.name }}NumberAggregateInput(TypedDict, total=False):
432410
{% else -%}
433411
{{ model.name }}RelationalFieldKeys = _NoneType
434412
{% endif %}
413+
414+
{% set other_models = all_models | selectattr('name', 'ne', model.name) | list %}
415+
{% if other_models %}
416+
# Cross-import related model types for resolution of forward references
417+
{% for other_model in other_models %}
418+
from .{{ other_model.name | lower }} import (
419+
{{ other_model.name }}WhereInput,
420+
{{ other_model.name }}WhereUniqueInput,
421+
{{ other_model.name }}OrderByInput,
422+
{{ other_model.name }}ScalarFieldKeys,
423+
{{ other_model.name }}RelationFilter,
424+
{{ other_model.name }}ListRelationFilter,
425+
{{ other_model.name }}CreateWithoutRelationsInput,
426+
{{ other_model.name }}CreateNestedWithoutRelationsInput,
427+
{{ other_model.name }}CreateManyNestedWithoutRelationsInput,
428+
{{ other_model.name }}UpdateOneWithoutRelationsInput,
429+
{{ other_model.name }}UpdateManyWithoutRelationsInput,
430+
{{ other_model.name }}ConnectOrCreateWithoutRelationsInput,
431+
)
432+
{% endfor %}
433+
{% endif %}

src/prisma/validator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import warnings
23
from types import ModuleType
34
from typing import Any, Type, TypeVar, cast, is_typeddict
45
from functools import lru_cache
@@ -8,6 +9,16 @@
89
from ._types import Protocol, runtime_checkable
910
from ._compat import PYDANTIC_V2, Extra
1011

12+
# Python 3.13+ raises a DeprecationWarning for ForwardRef._evaluate without type_params.
13+
# This is an issue in pydantic v1's typing module that we can't fix here.
14+
# Filter these warnings to avoid spurious failures.
15+
if sys.version_info >= (3, 13):
16+
warnings.filterwarnings(
17+
'ignore',
18+
message=r"Failing to pass a value to the 'type_params' parameter",
19+
category=DeprecationWarning,
20+
)
21+
1122
__all__ = ('validate',)
1223

1324
TD = TypeVar('TD')

0 commit comments

Comments
 (0)