-
Notifications
You must be signed in to change notification settings - Fork 119
Suggest user calling exec_type_checking in case of analysis errors #744
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
Open
ApusBerliozi
wants to merge
23
commits into
reagento:develop
Choose a base branch
from
ApusBerliozi:651_user_calling_exec_type_checking
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1d3bd38
feat(dishka): [#651] Added exec_type_checking to dishka
ApusBerliozi e4bc63f
feat: [#651] bumped _adaptix, added documentation
ApusBerliozi 32bfe36
feat: [#651] replaced List with list
ApusBerliozi 1f01a74
feat: [#651] bumped adaptix, corrected docs
ApusBerliozi c3844ee
feat: [#651] fixed pipelines
ApusBerliozi 6e4772c
feat: [#651] ignored mypy
ApusBerliozi 7479b09
feat: [#651] ignored mypy
ApusBerliozi 0d7fac8
feat: [#651] mypy errors
ApusBerliozi b371377
feat: [#651] mypy errors
ApusBerliozi 9cb617a
feat: [#651] mypy errors
ApusBerliozi ed37acb
feat: [#651] mypy errors
ApusBerliozi 4532ae0
feat: [#651] mypy errors
ApusBerliozi 9bf72b5
feat: [#651] mypy errors
ApusBerliozi f9ab3a1
feat: [#651] mypy errors
ApusBerliozi 707ac0b
feat: [#651] mypy errors
ApusBerliozi 932f5c7
feat: [#651] mypy errors
ApusBerliozi 733eeea
feat: [#651] mypy errors
ApusBerliozi d63b7be
feat: [#651] mypy errors
ApusBerliozi bc188df
feat: [#651] corrected mypy errors, de-bumped _adaptix, updated sugge…
ApusBerliozi 35ee2d1
feat: [#651] fixed typos
ApusBerliozi faec93b
feat: [#651] fixed typos
ApusBerliozi 4be4128
feat: [#651] fixed tests
ApusBerliozi d80df93
feat: [#651] fixed tests
ApusBerliozi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import ast | ||
| import inspect | ||
| from collections.abc import Callable, Sequence | ||
| from types import ModuleType | ||
|
|
||
|
|
||
| def _make_fragments_collector(*, typing_modules: Sequence[str]) \ | ||
| -> Callable[[ast.Module], list[ast.stmt]]: | ||
| def check_condition(expr: ast.expr) -> bool: | ||
| # searches for `TYPE_CHECKING` | ||
| if ( | ||
| isinstance(expr, ast.Name) | ||
| and isinstance(expr.ctx, ast.Load) | ||
| and expr.id == "TYPE_CHECKING" | ||
| ): | ||
| return True | ||
|
|
||
| # searches for `typing.TYPE_CHECKING` | ||
| if ( | ||
| isinstance(expr, ast.Attribute) | ||
| and expr.attr == "TYPE_CHECKING" | ||
| and isinstance(expr.ctx, ast.Load) | ||
| and isinstance(expr.value, ast.Name) | ||
| and expr.value.id in typing_modules | ||
| and isinstance(expr.value.ctx, ast.Load) | ||
| ): | ||
| return True | ||
| return False | ||
|
|
||
| def collect_type_checking_only_fragments(module: ast.Module) \ | ||
| -> list[ast.stmt]: | ||
| fragments = [] | ||
| for stmt in module.body: | ||
| if isinstance(stmt, ast.If) \ | ||
| and not stmt.orelse and check_condition(stmt.test): | ||
| fragments.extend(stmt.body) | ||
|
|
||
| return fragments | ||
|
|
||
| return collect_type_checking_only_fragments | ||
|
|
||
|
|
||
| default_collector = _make_fragments_collector(typing_modules=["typing"]) | ||
|
|
||
|
|
||
| def exec_type_checking( | ||
| module: ModuleType, | ||
| *, | ||
| collector: Callable[[ast.Module], list[ast.stmt]] = default_collector, | ||
| ) -> None: | ||
| """This function scans module source code, | ||
| collects fragments under ``if TYPE_CHECKING`` | ||
| and ``if typing.TYPE_CHECKING`` and executes them in the context of module. | ||
| After these, all imports and type definitions became | ||
| available at runtime for analysis. | ||
|
|
||
| By default, it ignores ``if`` with ``else`` branch. | ||
|
|
||
| :param module: A module for processing | ||
| :param collector: A function collecting code fragments to execute | ||
| """ | ||
| source = inspect.getsource(module) | ||
| fragments = collector(ast.parse(source)) | ||
| code = compile(ast.Module(fragments, type_ignores=[]), | ||
| f"<exec_type_checking of {module}>", "exec") | ||
| namespace = module.__dict__.copy() | ||
| exec(code, namespace) # noqa: S102 | ||
| for k, v in namespace.items(): | ||
| if not hasattr(module, k): | ||
| setattr(module, k, v) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/reagento/dishka/blob/develop/src/dishka/_adaptix/type_tools/type_evaler.py