Skip to content

Commit

Permalink
Nicer known-modules using flake8-pyproject
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaynx authored Sep 14, 2022
1 parent e7690eb commit 33f32b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ Real life example::
max-line-length = 100
known-modules = my-lib:[mylib.drm,mylib.encryption]

If you use `flake8-pyproject <https://pypi.org/project/Flake8-pyproject/>`_, you can also configure
the known modules using a nicer syntax::

$ cat pyproject.toml
...
[tool.flake8]
max-line-length = 100

[tool.flake8.known-modules]
my-lib = ["mylib.drm", "mylib.encryption"]

It is also possible to scan host's site-packages directory for installed packages. This feature is
disabled by default, but user can enable it with the ``--scan-host-site-packages`` command line
option. Please note, however, that the location of the site-packages directory will be determined
Expand Down
22 changes: 14 additions & 8 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .modules import STDLIB_PY3

# NOTE: Changing this number will alter package version as well.
__version__ = "1.6.2"
__version__ = "1.7.0"
__license__ = "MIT"

LOG = getLogger('flake8.plugin.requirements')
Expand Down Expand Up @@ -375,13 +375,19 @@ def add_options(cls, manager):
@classmethod
def parse_options(cls, options):
"""Parse plug-in specific options."""
cls.known_modules = {
project2module(k): v.split(",")
for k, v in [
x.split(":[")
for x in re.split(r"],?", options.known_modules)[:-1]
]
}
if isinstance(options.known_modules, dict):
# Support for nicer known-modules using flake8-pyproject.
cls.known_modules = {
project2module(k): v for k, v in options.known_modules.items()
}
else:
cls.known_modules = {
project2module(k): v.split(",")
for k, v in [
x.split(":[")
for x in re.split(r"],?", options.known_modules)[:-1]
]
}
cls.requirements_file = options.requirements_file
cls.requirements_max_depth = options.requirements_max_depth
if options.scan_host_site_packages:
Expand Down
2 changes: 1 addition & 1 deletion test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def processing_setup_py(self):
class Flake8Options:
known_modules = ""
requirements_file = None
requirements_max_depth = 0
requirements_max_depth = 1
scan_host_site_packages = False


Expand Down
19 changes: 19 additions & 0 deletions test/test_pep621.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
builtins_open = '__builtin__.open'


class Flake8Options:
known_modules = ""
requirements_file = None
requirements_max_depth = 1
scan_host_site_packages = False


class Pep621TestCase(unittest.TestCase):

content = """
Expand All @@ -26,6 +33,18 @@ class Pep621TestCase(unittest.TestCase):
def setUp(self):
memoize.mem = {}

def tearDown(self):
Flake8Checker.root_dir = ""

def test_pyproject_custom_mapping_parser(self):
class Options(Flake8Options):
known_modules = {"mylib": ["mylib.drm", "mylib.ex"]}
Flake8Checker.parse_options(Options)
self.assertEqual(
Flake8Checker.known_modules,
{"mylib": ["mylib.drm", "mylib.ex"]},
)

def test_get_pyproject_toml_pep621(self):
with mock.patch(builtins_open, mock.mock_open(read_data=self.content)):
pep621 = Flake8Checker.get_pyproject_toml_pep621()
Expand Down

0 comments on commit 33f32b4

Please sign in to comment.