-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Apply mypy-tests custom config to other mypy-based tests #13825
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
srittau
merged 12 commits into
python:main
from
Avasam:Apply-mypy-tests-custom-config-to-other-mypy-based-tests
Apr 28, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b3011c8
Apply mypy-tests custom config to other mypy-based tests
Avasam 4624afd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d637e45
Refresh with more updated main
Avasam 3f97ef1
Merge branch 'Apply-mypy-tests-custom-config-to-other-mypy-based-test…
Avasam 0111d5d
Move MypyResult back to tests/mypy_test.py
Avasam ae7cc8d
Try extracting NamedTemporaryFile workaround
Avasam 23b2d02
Leftover remove in temporary_mypy_config_file
Avasam 4ee3ac5
Simplify self.close() call
Avasam 378f669
Use self.name
Avasam 99c6260
Flip close and remove
Avasam fc55a22
Close can't use self, because self-referenctial
Avasam 2dc0a2a
Remove extra comment and fix pyright issues
Avasam 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,64 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator, Iterable | ||
from contextlib import contextmanager | ||
from typing import Any, NamedTuple | ||
|
||
import tomli | ||
|
||
from ts_utils.metadata import metadata_path | ||
from ts_utils.utils import NamedTemporaryFile, TemporaryFileWrapper | ||
|
||
|
||
class MypyDistConf(NamedTuple): | ||
module_name: str | ||
values: dict[str, dict[str, Any]] | ||
|
||
|
||
# The configuration section in the metadata file looks like the following, with multiple module sections possible | ||
# [mypy-tests] | ||
# [mypy-tests.yaml] | ||
# module_name = "yaml" | ||
# [mypy-tests.yaml.values] | ||
# disallow_incomplete_defs = true | ||
# disallow_untyped_defs = true | ||
|
||
|
||
def mypy_configuration_from_distribution(distribution: str) -> list[MypyDistConf]: | ||
with metadata_path(distribution).open("rb") as f: | ||
data = tomli.load(f) | ||
|
||
# TODO: This could be added to ts_utils.metadata | ||
mypy_tests_conf: dict[str, dict[str, Any]] = data.get("mypy-tests", {}) | ||
if not mypy_tests_conf: | ||
return [] | ||
|
||
def validate_configuration(section_name: str, mypy_section: dict[str, Any]) -> MypyDistConf: | ||
assert isinstance(mypy_section, dict), f"{section_name} should be a section" | ||
module_name = mypy_section.get("module_name") | ||
|
||
assert module_name is not None, f"{section_name} should have a module_name key" | ||
assert isinstance(module_name, str), f"{section_name} should be a key-value pair" | ||
|
||
assert "values" in mypy_section, f"{section_name} should have a values section" | ||
values: dict[str, dict[str, Any]] = mypy_section["values"] | ||
assert isinstance(values, dict), "values should be a section" | ||
return MypyDistConf(module_name, values.copy()) | ||
|
||
assert isinstance(mypy_tests_conf, dict), "mypy-tests should be a section" | ||
return [validate_configuration(section_name, mypy_section) for section_name, mypy_section in mypy_tests_conf.items()] | ||
|
||
|
||
@contextmanager | ||
def temporary_mypy_config_file(configurations: Iterable[MypyDistConf]) -> Generator[TemporaryFileWrapper[str]]: | ||
temp = NamedTemporaryFile("w+") | ||
try: | ||
for dist_conf in configurations: | ||
temp.write(f"[mypy-{dist_conf.module_name}]\n") | ||
for k, v in dist_conf.values.items(): | ||
temp.write(f"{k} = {v}\n") | ||
temp.write("[mypy]\n") | ||
temp.flush() | ||
yield temp | ||
finally: | ||
temp.close() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe this should be a docstring ?
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.
I actually prefer this to be a comment. It doesn't really feel like something that's relevant to the API. That said, it would probably be better to document it in
CONTRIBUTING
instead.