Skip to content

Commit

Permalink
Refine typing of conditional imports
Browse files Browse the repository at this point in the history
Using ModuleType is a little heavy-handed. Avoiding it, a lighter
touch, better preserves information about the optional modules.
  • Loading branch information
sirosen committed May 31, 2024
1 parent 63e0a04 commit 51da5c1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 4 additions & 5 deletions nose2/plugins/loader/eggdiscovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import logging
import os
import types

from nose2 import events
from nose2.plugins.loader import discovery
Expand All @@ -26,11 +25,11 @@
log = logging.getLogger(__name__)

try:
import pkg_resources as _pkg_resources_mod
import pkg_resources

pkg_resources: types.ModuleType | None = _pkg_resources_mod
_has_pkg_resources = True
except ImportError:
pkg_resources = None
_has_pkg_resources = False


class EggDiscoveryLoader(events.Plugin, discovery.Discoverer):
Expand Down Expand Up @@ -85,7 +84,7 @@ def _find_tests_in_egg_dir(self, event, rel_path, dist):
def _find_tests_in_dir(self, event, full_path, top_level):
if os.path.exists(full_path):
return
elif pkg_resources and full_path.find(".egg") != -1:
elif _has_pkg_resources and full_path.find(".egg") != -1:
egg_path = full_path.split(".egg")[0] + ".egg"
for dist in pkg_resources.find_distributions(egg_path):
for modname in dist._get_metadata("top_level.txt"):
Expand Down
6 changes: 2 additions & 4 deletions nose2/tests/functional/test_eggdiscovery_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from nose2.tests._common import FunctionalTestCase, support_file

try:
import pkg_resources

import pkg_resources # noqa: F401
except ImportError:
pkg_resources = None

pass
else:

class EggDiscoveryFunctionalTest(FunctionalTestCase):
Expand Down

0 comments on commit 51da5c1

Please sign in to comment.