Skip to content

Commit a2b4459

Browse files
matejspMatej Spiller Muys
and
Matej Spiller Muys
authored
Support for python 3.12 datetime (#427)
Co-authored-by: Matej Spiller Muys <[email protected]>
1 parent 955f779 commit a2b4459

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This version drops support for Python 3.6
1212
Bugfixes
1313
~~~~~~~~
1414

15+
- Fixed compatibility issue with datetime classes and python `3.12` (`#425 <https://github.com/pylint-dev/pylint-django/issues/425>`_)
1516

1617

1718
Version 2.5.3 (25 Mär 2022)

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
- [naquiroz](https://github.com/naquiroz)
2121
- [john-sandall](https://github.com/john-sandall)
2222
- [dineshtrivedi](https://github.com/dineshtrivedi)
23+
- [matejsp](https://github.com/matejsp)

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)