Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for python 3.12 datetime #427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -12,6 +12,7 @@ This version drops support for Python 3.6
Bugfixes
~~~~~~~~

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


Version 2.5.3 (25 Mär 2022)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Expand Up @@ -20,3 +20,4 @@
- [naquiroz](https://github.com/naquiroz)
- [john-sandall](https://github.com/john-sandall)
- [dineshtrivedi](https://github.com/dineshtrivedi)
- [matejsp](https://github.com/matejsp)
4 changes: 4 additions & 0 deletions pylint_django/compat.py
@@ -1,6 +1,7 @@
# flake8: noqa
# pylint: skip-file
# no sane linter can figure out the hackiness in this compatibility layer...
import sys

try:
from astroid.nodes import AssignName, Attribute, ClassDef, FunctionDef, ImportFrom
Expand Down Expand Up @@ -33,3 +34,6 @@
LOAD_CONFIGURATION_SUPPORTED = tuple(pylint.__version__.split(".")) >= ("2", "3")
except AttributeError:
LOAD_CONFIGURATION_SUPPORTED = pylint.__pkginfo__.numversion >= (2, 3)

# datetime module is compiled and moved to _pydatetime
COMPILED_DATETIME_CLASSES = sys.version_info >= (3, 12)
24 changes: 18 additions & 6 deletions pylint_django/transforms/fields.py
@@ -1,7 +1,7 @@
from astroid import MANAGER, AstroidImportError, inference_tip, nodes
from astroid.nodes import scoped_nodes

from pylint_django import utils
from pylint_django import compat, utils

_STR_FIELDS = (
"CharField",
Expand Down Expand Up @@ -46,7 +46,7 @@ def is_model_or_form_field(cls):
return is_model_field(cls) or is_form_field(cls)


def apply_type_shim(cls, _context=None):
def apply_type_shim(cls, _context=None): # pylint: disable=too-many-statements
if cls.name in _STR_FIELDS:
base_nodes = scoped_nodes.builtin_lookup("str")
elif cls.name in _INT_FIELDS:
Expand All @@ -61,13 +61,25 @@ def apply_type_shim(cls, _context=None):
except AstroidImportError:
base_nodes = MANAGER.ast_from_module_name("_pydecimal").lookup("Decimal")
elif cls.name in ("SplitDateTimeField", "DateTimeField"):
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
if compat.COMPILED_DATETIME_CLASSES:
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("datetime")
else:
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("datetime")
elif cls.name == "TimeField":
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
if compat.COMPILED_DATETIME_CLASSES:
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("time")
else:
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("time")
elif cls.name == "DateField":
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
if compat.COMPILED_DATETIME_CLASSES:
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("date")
else:
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("date")
elif cls.name == "DurationField":
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
if compat.COMPILED_DATETIME_CLASSES:
base_nodes = MANAGER.ast_from_module_name("_pydatetime").lookup("timedelta")
else:
base_nodes = MANAGER.ast_from_module_name("datetime").lookup("timedelta")
elif cls.name == "UUIDField":
base_nodes = MANAGER.ast_from_module_name("uuid").lookup("UUID")
elif cls.name == "ManyToManyField":
Expand Down