-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add a get_additional_deps hook to Plugin to support django-stubs #6598
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -371,6 +371,23 @@ def lookup_fully_qualified(self, fullname: str) -> Optional[SymbolTableNode]: | |
assert self._modules is not None | ||
return lookup_fully_qualified(fullname, self._modules) | ||
|
||
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]: | ||
"""Customize dependencies for a module. | ||
|
||
This hook allows adding in new dependencies for a module. It | ||
is called after parsing a file but before analysis. This can | ||
be useful if a library has dependencies that are dynamic based | ||
on configuration information, for example. | ||
|
||
Returns a list of (priority, module name, line number) tuples. | ||
|
||
The line number can be -1 when there is not a known real line number. | ||
|
||
Priorities are defined in mypy.build (but maybe shouldn't be). | ||
10 is a good choice for priority. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (+ an example) should be also added to @msullivan Will you have time to make a PR? I suppose this will be announced in the release blog post, it would be great to have some docs to link to. |
||
return [] | ||
|
||
def get_type_analyze_hook(self, fullname: str | ||
) -> Optional[Callable[[AnalyzeTypeContext], Type]]: | ||
"""Customize behaviour of the type analyzer for given full names. | ||
|
@@ -395,7 +412,7 @@ def get_function_hook(self, fullname: str | |
"""Adjust the return type of a function call. | ||
|
||
This method is called after type checking a call. Plugin may adjust the return | ||
type inferred by mypy, and/or emmit some error messages. Note, this hook is also | ||
type inferred by mypy, and/or emit some error messages. Note, this hook is also | ||
called for class instantiation calls, so that in this example: | ||
|
||
from lib import Class, do_stuff | ||
|
@@ -561,6 +578,9 @@ def set_modules(self, modules: Dict[str, MypyFile]) -> None: | |
def lookup_fully_qualified(self, fullname: str) -> Optional[SymbolTableNode]: | ||
return self.plugin.lookup_fully_qualified(fullname) | ||
|
||
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]: | ||
return self.plugin.get_additional_deps(file) | ||
|
||
def get_type_analyze_hook(self, fullname: str | ||
) -> Optional[Callable[[AnalyzeTypeContext], Type]]: | ||
return self.plugin.get_type_analyze_hook(fullname) | ||
|
@@ -626,6 +646,12 @@ def set_modules(self, modules: Dict[str, MypyFile]) -> None: | |
for plugin in self._plugins: | ||
plugin.set_modules(modules) | ||
|
||
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]: | ||
deps = [] | ||
for plugin in self._plugins: | ||
deps.extend(plugin.get_additional_deps(file)) | ||
return deps | ||
|
||
def get_type_analyze_hook(self, fullname: str | ||
) -> Optional[Callable[[AnalyzeTypeContext], Type]]: | ||
return self._find_hook(lambda plugin: plugin.get_type_analyze_hook(fullname)) | ||
|
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,15 @@ | ||
from typing import Optional, Callable, List, Tuple | ||
|
||
from mypy.plugin import Plugin | ||
from mypy.nodes import MypyFile | ||
|
||
|
||
class DepsPlugin(Plugin): | ||
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]: | ||
if file.fullname() == '__main__': | ||
return [(10, 'err', -1)] | ||
return [] | ||
|
||
|
||
def plugin(version): | ||
return DepsPlugin |
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.
Uh oh!
There was an error while loading. Please reload this page.