Skip to content

Commit b60fc57

Browse files
author
Matej Spiller Muys
committed
Support for python 3.12 datetime
1 parent 955f779 commit b60fc57

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

pylint_django/compat.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# flake8: noqa
22
# pylint: skip-file
33
# no sane linter can figure out the hackiness in this compatibility layer...
4+
import sys
45

56
try:
67
from astroid.nodes import AssignName, Attribute, ClassDef, FunctionDef, ImportFrom
@@ -33,3 +34,6 @@
3334
LOAD_CONFIGURATION_SUPPORTED = tuple(pylint.__version__.split(".")) >= ("2", "3")
3435
except AttributeError:
3536
LOAD_CONFIGURATION_SUPPORTED = pylint.__pkginfo__.numversion >= (2, 3)
37+
38+
# datetime module is compiled and moved to _pydatetime
39+
COMPILED_DATETIME_CLASSES = sys.version_info >= (3, 12)

pylint_django/transforms/fields.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from astroid import MANAGER, AstroidImportError, inference_tip, nodes
22
from astroid.nodes import scoped_nodes
33

4-
from pylint_django import utils
4+
from pylint_django import compat, utils
55

66
_STR_FIELDS = (
77
"CharField",
@@ -46,7 +46,7 @@ def is_model_or_form_field(cls):
4646
return is_model_field(cls) or is_form_field(cls)
4747

4848

49-
def apply_type_shim(cls, _context=None):
49+
def apply_type_shim(cls, _context=None): # pylint: disable=too-many-statements
5050
if cls.name in _STR_FIELDS:
5151
base_nodes = scoped_nodes.builtin_lookup("str")
5252
elif cls.name in _INT_FIELDS:
@@ -61,13 +61,25 @@ def apply_type_shim(cls, _context=None):
6161
except AstroidImportError:
6262
base_nodes = MANAGER.ast_from_module_name("_pydecimal").lookup("Decimal")
6363
elif cls.name in ("SplitDateTimeField", "DateTimeField"):
64-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
64+
if compat.COMPILED_DATETIME_CLASSES:
65+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("datetime")
66+
else:
67+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
6568
elif cls.name == "TimeField":
66-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
69+
if compat.COMPILED_DATETIME_CLASSES:
70+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("time")
71+
else:
72+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
6773
elif cls.name == "DateField":
68-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
74+
if compat.COMPILED_DATETIME_CLASSES:
75+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("date")
76+
else:
77+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
6978
elif cls.name == "DurationField":
70-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
79+
if compat.COMPILED_DATETIME_CLASSES:
80+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("timedelta")
81+
else:
82+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
7183
elif cls.name == "UUIDField":
7284
base_nodes = MANAGER.ast_from_module_name("uuid").lookup("UUID")
7385
elif cls.name == "ManyToManyField":

0 commit comments

Comments
 (0)