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

Resolve import conflicts while generating stubs #302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Feuermurmel
Copy link
Contributor

It tries to solve #209. I don't know if this is a good solution but it solved the problem in my case. Look at test_multiple_imported_symbols_same_name() to get an idea of what this does.

The basic idea it to do rendering of a module stub in two passes. ModuleStub.render() first calls ImportBlockStub.resolve_import_conflicts(), which generates a set of import renames, if necessary. This is stored in an instance of RenderContext. Then this context is passed to all methods relating to rendering the stubs which adjust imports and usages of affected symbols accordingly.

IDK, tell me what you think.

@carljm
Copy link
Contributor

carljm commented Sep 15, 2023

Thanks for working on this! May take me a bit to find the time to review it, sorry.

@carljm
Copy link
Contributor

carljm commented Sep 15, 2023

Looks like this does need Black run over it, too.

@Feuermurmel
Copy link
Contributor Author

Looks like this does need Black run over it, too.

I'm a bit confused. If I run black and isort, I get a lot of changes on files I didn't touch:

$ pipenv run black .
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/conftest.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/db/test_base.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/doc/conf.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/demo/models.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/db/test_sqlite.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_config.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/demo/inbox.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/demo/test_inbox.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_type_checking_imports_transformer.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_util.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_encoding.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/util.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/monkeytype/cli.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_tracing.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_cli.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/monkeytype/stubs.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_typing.py
reformatted /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_stubs.py

All done! ✨ 🍰 ✨
18 files reformatted, 22 files left unchanged.
$ pipenv run isort .
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/demo/models.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/monkeytype/stubs.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/monkeytype/cli.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_stubs.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_tracing.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/util.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_typing.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_encoding.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_util.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_config.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_type_checking_imports_transformer.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/test_cli.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/db/test_base.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/tests/db/test_sqlite.py
Fixing /Users/cs/Documents/Cloudscale/Projects/captain/stuff/MonkeyType/doc/conf.py
Skipped 3 files

What's the expectation here?

@carljm
Copy link
Contributor

carljm commented Sep 20, 2023

The CI only runs black/isort on files within the monkeytype/ directory (see e.g. the tox.ini or .github/workflows/test.yml), so you should do the same. It looks like all the "spurious" changes you are seeing are in tests/, demo/, or docs/ -- don't submit those changes. Thanks!

@Feuermurmel
Copy link
Contributor Author

The CI only runs black/isort on files within the monkeytype/ directory (see e.g. the tox.ini or .github/workflows/test.yml), so you should do the same.

Ah, thanks for the hint. It should be fixed now!

I saw black and isort mentioned in setup.cfg. I tried running those and noticed that only part of the files are formatted. Then I searched for black and isort to find instructions on which commands to run before committing (would have expected that to be in CONTRIBUTING.rst or there being a pre-commit-config.yaml file) and didn't find anything, so I gave up at that point and just opened this MR, waiting for the CI to tell me what I need to do.

Maybe add black monkeytype && isort monkeytype to CONTRIBUTING.rst or add a pre-commit-config.yaml file (or similar tooling)?

@carljm
Copy link
Contributor

carljm commented Sep 21, 2023

Not sure why black/isort weren't already mentioned in CONTRIBUTING.rst; thanks for pointing that out! Fixed in 49fc029

Adding pre-commit support sounds like a good idea, filed #308 to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants