Skip to content

Harden isclass? #4266

@blueyed

Description

@blueyed

In pytest-dev/pytest-django#668 we just learned that Django raises ImproperlyConfigured already when isinstance is called on settings.

I wonder if pytest's isclass should be hardened in this regard to return False.

But the real fix would probably be for Django to exclude __class__ in the wrapped __getattr__ (or whatever triggers this).

What do you think?

In [10]: from django.conf import settings as django_settings

In [11]: isinstance(django_settings, type)
---------------------------------------------------------------------------
ImproperlyConfigured                      Traceback (most recent call last)
<ipython-input-11-9824ac31307c> in <module>()
----> 1 isinstance(django_settings, type)

/usr/lib/python3.7/site-packages/django/utils/functional.py in inner(self, *args)
    211     def inner(self, *args):
    212         if self._wrapped is empty:
--> 213             self._setup()
    214         return func(self._wrapped, *args)
    215     return inner

/usr/lib/python3.7/site-packages/django/conf/__init__.py in _setup(self, name)
     40                 "You must either define the environment variable %s "
     41                 "or call settings.configure() before accessing settings."
---> 42                 % (desc, ENVIRONMENT_VARIABLE))
     43 
     44         self._wrapped = Settings(settings_module)

ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions