Skip to content

Commit

Permalink
Allow exception modules to have any special characters (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwarren2 committed Sep 18, 2020
1 parent 2255ce8 commit 43ffbee
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Contributors

* Anthony Sottile - https://github.com/asottile
* Łukasz Skarżyński - https://github.com/skarzi
* Daniel Jurczak - https://github.com/danieljurczak
* Daniel Jurczak - https://github.com/danieljurczak
* Ben Warren - https://github.com/bwarren2
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ latest
* Upgrade Grimp to 1.2.2.
* Add SetField
* Use a SetField for ignore_imports options
* Add support for non `\w` characters in import exceptions
8 changes: 2 additions & 6 deletions src/importlinter/domain/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import abc
import re
from typing import Generic, Iterable, List, Set, TypeVar, Union

from importlinter.domain.imports import DirectImport, Module
Expand Down Expand Up @@ -117,12 +116,9 @@ class DirectImportField(Field):
Expects raw data in the form: "mypackage.foo.importer -> mypackage.bar.imported".
"""

DIRECT_IMPORT_STRING_REGEX = re.compile(r"^([\w\.]+) -> ([\w\.]+)$")

def parse(self, raw_data: Union[str, List]) -> DirectImport:
string = StringField().parse(raw_data)
match = self.DIRECT_IMPORT_STRING_REGEX.match(string)
if not match:
importer, _, imported = string.partition(" -> ")
if not (importer and imported):
raise ValidationError('Must be in the form "package.importer -> package.imported".')
importer, imported = match.groups()
return DirectImport(importer=Module(importer), imported=Module(imported))
4 changes: 4 additions & 0 deletions tests/unit/domain/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class TestModuleField(BaseFieldTest):
"mypackage.foo - mypackage.bar",
ValidationError('Must be in the form "package.importer -> package.imported".'),
),
(
"my-package.foo -> my-package.bar",
DirectImport(importer=Module("my-package.foo"), imported=Module("my-package.bar")),
),
),
)
class TestDirectImportField(BaseFieldTest):
Expand Down

0 comments on commit 43ffbee

Please sign in to comment.