From 6c310c666ea4162b17940eef9a93028d8d3e3741 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 13 Jul 2022 22:00:45 -0400 Subject: [PATCH] Initial attempt at trying to correct general import errors based on Python/Django version Currently gives errors in Python3.8 and Django >= 4.0. --- django_dump_die/templatetags/dump_die.py | 11 ++++++++--- django_dump_die/utils.py | 10 +++++++--- django_dump_die/views/example_helpers.py | 11 +++++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/django_dump_die/templatetags/dump_die.py b/django_dump_die/templatetags/dump_die.py index 3be9af1..e5fc4f5 100644 --- a/django_dump_die/templatetags/dump_die.py +++ b/django_dump_die/templatetags/dump_die.py @@ -1,7 +1,6 @@ """Template Tags for DumpDie""" import inspect -import pytz import re import types @@ -25,6 +24,7 @@ from django_dump_die.constants import ( INCLUDE_MAGIC_METHODS, INCLUDE_ATTRIBUTES, INCLUDE_FUNCTIONS, + PYTZ_PRESENT, ) from django_dump_die.utils import ( @@ -46,6 +46,11 @@ from django_dump_die.utils import ( is_magic, ) +# Imports that may not be accessible, depending on local python environment setup. +if PYTZ_PRESENT: + import pytz + + register = template.Library() @@ -135,7 +140,7 @@ def dump_object( # OR if direct parent is a intermediate (excluding pytz timezone objects). elif ( _is_simple_type(obj) - or (parent_is_intermediate and not isinstance(obj, pytz.BaseTzInfo)) + or (PYTZ_PRESENT and parent_is_intermediate and not isinstance(obj, pytz.BaseTzInfo)) ): return _handle_simple_type(obj) @@ -341,7 +346,7 @@ def _is_intermediate_type(obj): """Return if the obj is an intermediate type.""" # Special handling for pytz timezone objects. - if isinstance(obj, pytz.BaseTzInfo): + if PYTZ_PRESENT and isinstance(obj, pytz.BaseTzInfo): return True # Handling for all other objects. diff --git a/django_dump_die/utils.py b/django_dump_die/utils.py index 473b5f5..07d8334 100644 --- a/django_dump_die/utils.py +++ b/django_dump_die/utils.py @@ -5,7 +5,6 @@ import inspect import linecache import tokenize -import pytz import io import re from tokenize import ( @@ -24,7 +23,12 @@ from enum import EnumMeta from django.core.exceptions import ObjectDoesNotExist -from django_dump_die.constants import INCLUDE_FILENAME_LINENUMBER, COLORIZE_DUMPED_OBJECT_NAME +from django_dump_die.constants import COLORIZE_DUMPED_OBJECT_NAME, INCLUDE_FILENAME_LINENUMBER, PYTZ_PRESENT + +# Imports that may not be accessible, depending on local python environment setup. +if PYTZ_PRESENT: + import pytz + class Enum: """Enum faker class so an entire Enum can be dumped correctly.""" @@ -394,7 +398,7 @@ def get_obj_type(obj): # Special handling for certain types. if obj_type == 'NoneType': obj_type = 'null' - elif isinstance(obj, pytz.BaseTzInfo): + elif PYTZ_PRESENT and isinstance(obj, pytz.BaseTzInfo): obj_type = 'pytz_timezone' return obj_type diff --git a/django_dump_die/views/example_helpers.py b/django_dump_die/views/example_helpers.py index 13bf84d..743aac3 100644 --- a/django_dump_die/views/example_helpers.py +++ b/django_dump_die/views/example_helpers.py @@ -6,7 +6,6 @@ to general package usage. """ from enum import Enum -import pytz import os from datetime import datetime, timedelta from decimal import Decimal @@ -17,8 +16,10 @@ from django.utils import timezone from pathlib import Path, PosixPath, PurePath, WindowsPath from types import ModuleType -from django_dump_die.constants import ZONEINFO_PRESENT +from django_dump_die.constants import PYTZ_PRESENT, ZONEINFO_PRESENT +if PYTZ_PRESENT: + import pytz if ZONEINFO_PRESENT: from zoneinfo import ZoneInfo @@ -603,7 +604,8 @@ def dump_datetime_types(): sample_tz_time = timezone.now().time() sample_dt_timedelta = timedelta(days=1) sample_tz_timedelta = timezone.timedelta(days=2) - sample_pytz_timezone = pytz.timezone('UTC') + if PYTZ_PRESENT: + sample_pytz_timezone = pytz.timezone('UTC') if ZONEINFO_PRESENT: sample_zoneinfo_timezone = ZoneInfo('UTC') @@ -618,7 +620,8 @@ def dump_datetime_types(): dump(sample_tz_time) dump(sample_dt_timedelta) dump(sample_tz_timedelta) - dump(sample_pytz_timezone) + if PYTZ_PRESENT: + dump(sample_pytz_timezone) if ZONEINFO_PRESENT: dump(sample_zoneinfo_timezone) -- GitLab