Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an exact import bug with deeper nesting #2089

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,15 @@ def relative(current_module: str, reference: str) -> Tuple[str, str]:
return left, right


# def absolute()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove it?



def exact_import(from_: str, import_: str, short_name: str) -> Tuple[str, str]:
if from_ == '.':
if from_ == len(from_) * '.':
# Prevents "from . import foo" becoming "from ..foo import Foo"
# or "from .. import foo" becoming "from ...foo import Foo"
# when our imported module has the same parent
return f'.{import_}', short_name
return f'{from_}{import_}', short_name
return f'{from_}.{import_}', short_name


Expand Down
2 changes: 1 addition & 1 deletion docs/development-contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ cd datamodel-code-generator
$ curl -sSL https://install.python-poetry.org | python3 -

## 3. Install dependencies
$ poetry install
$ poetry install --all-extras
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests.sh fails without graphql installed


## 4. Create new branch and rewrite code.
$ git checkout -b new-branch
Expand Down
1 change: 1 addition & 0 deletions tests/parser/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def test_relative(current_module: str, reference: str, val: Tuple[str, str]):
'from_,import_,name,val',
[
('.', 'mod', 'Foo', ('.mod', 'Foo')),
('..', 'mod', 'Foo', ('..mod', 'Foo')),
('.a', 'mod', 'Foo', ('.a.mod', 'Foo')),
('..a', 'mod', 'Foo', ('..a.mod', 'Foo')),
('..a.b', 'mod', 'Foo', ('..a.b.mod', 'Foo')),
Expand Down
Loading