Skip to content

Commit cccd9b9

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

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pylint_django/compat.py

Lines changed: 4 additions & 0 deletions
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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from astroid import MANAGER, AstroidImportError, inference_tip, nodes
22
from astroid.nodes import scoped_nodes
33

4+
from pylint_django import compat
45
from pylint_django import utils
56

67
_STR_FIELDS = (
@@ -46,7 +47,7 @@ def is_model_or_form_field(cls):
4647
return is_model_field(cls) or is_form_field(cls)
4748

4849

49-
def apply_type_shim(cls, _context=None):
50+
def apply_type_shim(cls, _context=None): # pylint: disable=too-many-statements
5051
if cls.name in _STR_FIELDS:
5152
base_nodes = scoped_nodes.builtin_lookup("str")
5253
elif cls.name in _INT_FIELDS:
@@ -61,13 +62,25 @@ def apply_type_shim(cls, _context=None):
6162
except AstroidImportError:
6263
base_nodes = MANAGER.ast_from_module_name("_pydecimal").lookup("Decimal")
6364
elif cls.name in ("SplitDateTimeField", "DateTimeField"):
64-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
65+
if compat.COMPILED_DATETIME_CLASSES:
66+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("datetime")
67+
else:
68+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
6569
elif cls.name == "TimeField":
66-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
70+
if compat.COMPILED_DATETIME_CLASSES:
71+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("time")
72+
else:
73+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
6774
elif cls.name == "DateField":
68-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
75+
if compat.COMPILED_DATETIME_CLASSES:
76+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("date")
77+
else:
78+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
6979
elif cls.name == "DurationField":
70-
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
80+
if compat.COMPILED_DATETIME_CLASSES:
81+
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("timedelta")
82+
else:
83+
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
7184
elif cls.name == "UUIDField":
7285
base_nodes = MANAGER.ast_from_module_name("uuid").lookup("UUID")
7386
elif cls.name == "ManyToManyField":

0 commit comments

Comments
 (0)