Skip to content

Commit ff7ef94

Browse files
authored
fix: improve type resolution for model field declarations (#2216)
* fix: improve type resolution for model field declarations * Updated model field declarations to resolve to their concrete types (e.g., `CharField[str]`) in Pyright/Pylance, enhancing type-checking accuracy. * Modified the `Field.__new__` type-check stub to return `Self`, improving type inference. This change addresses type-checking issues and improves developer experience with IDEs. * Update CHANGELOG with reference to the issue
1 parent cd2e9fe commit ff7ef94

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed
2222
^^^^^
2323
- ``MigrationRecorder`` now uses parameterized queries; fixes MariaDB/MySQL rejecting ISO-8601 ``applied_at`` values. (#2132)
2424
- ``QuerySet.count()`` now matches the limited query result for the LIMIT/OFFSET edge cases: it returns ``0`` (instead of a negative number) when ``offset()`` exceeds the total row count, and ``0`` (instead of the total) for ``limit(0)``. (#2208)
25+
- Field declarations on models now resolve to their concrete type (e.g. ``CharField[str]``) in Pyright/Pylance instead of ``Field[Unknown]``; the ``Field.__new__`` type-check stub now returns ``Self``. (#2216)
2526

2627
1.1.7
2728
-----

tortoise/fields/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
if sys.version_info >= (3, 11):
2121
from enum import StrEnum
22+
from typing import Self
2223
else: # pragma: no cover
24+
from typing_extensions import Self
2325

2426
class StrEnum(str, Enum):
2527
__str__ = str.__str__
@@ -202,7 +204,7 @@ def function_cast(self, term: Term) -> Term:
202204
# These methods are just to make IDE/Linters happy:
203205
if TYPE_CHECKING:
204206

205-
def __new__(cls, *args: Any, **kwargs: Any) -> Field[VALUE]:
207+
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
206208
return super().__new__(cls)
207209

208210
@overload

0 commit comments

Comments
 (0)