Skip to content

Commit

Permalink
Document "known-modules" option
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jul 13, 2019
1 parent 6bec368 commit 1b7c5d2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2017-2018 Arkadiusz Bokowy <[email protected]>
Copyright (c) 2017-2019 Arkadiusz Bokowy <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ You can install, upgrade, or uninstall ``flake8-requirements`` with these comman
$ pip install flake8-requirements
$ pip install --upgrade flake8-requirements
$ pip uninstall flake8-requirements

Customization
-------------

For projects with custom (private) dependencies, one can provide mapping between project name and
provided modules. Such a mapping can be set on the command line during the flake8 invocation with
the ``--known-modules`` option or alternatively in the ``[flake8]`` section of the configuration
file, e.g. ``setup.cfg``. The syntax of the custom mapping looks like follows::

1st-project-name:[module1,module2,...],2nd-project-name:[moduleA,moduleB,...],...

If some local project lacks "name" attribute in the ``setup.py`` file (it is highly discouraged
not to provide the "name" attribute, though), one can omit the project name in the mapping and do
as follows::

:[localmodule1,localmodule2,...],1st-local-library:[moduleA,moduleB,...],...

Real life example::

$ cat setup.cfg
[flake8]
max-line-length = 100
known-modules = my-lib:[mylib.drm,mylib.encryption]
6 changes: 3 additions & 3 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from .modules import STDLIB_PY3

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

LOG = getLogger('flake8.plugin.requires')
LOG = getLogger('flake8.plugin.requirements')

ERRORS = {
'I900': "I900 '{pkg}' not listed as a requirement",
Expand Down Expand Up @@ -246,7 +246,7 @@ def visit_Call(self, node):
class Flake8Checker(object):
"""Package requirements checker."""

name = "flake8-requires"
name = "flake8-requirements"
version = __version__

# User defined project->modules mapping.
Expand Down
2 changes: 1 addition & 1 deletion src/flake8_requirements/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@
# or the name of the module is different than the project name itself.
KNOWN_3RD_PARTIES = {
"awesome_slugify": ["slugify"],
"beautifulsoup4": ["bs4"]
"beautifulsoup4": ["bs4"],
"cx_oracle": ["cx_Oracle"],
"enum34": ["enum"],
"factory_boy": ["factory"],
Expand Down
16 changes: 16 additions & 0 deletions test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ def test_relative(self):
errors = check("from ..local import local")
self.assertEqual(len(errors), 0)

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

def test_custom_mapping(self):
class Flake8Options:
known_modules = "flake8-requires:[flake8req]"
Flake8Checker.parse_options(Flake8Options)
errors = check("from flake8req import mymodule")
self.assertEqual(len(errors), 0)

def test_setup_py(self):
errors = check("from setuptools import setup", "setup.py")
self.assertEqual(len(errors), 0)
Expand Down

0 comments on commit 1b7c5d2

Please sign in to comment.