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

Avoid Mypy error when there's an __init__.py in the repo root #499

Merged
merged 4 commits into from
May 31, 2023

Conversation

akaihola
Copy link
Owner

Fixes #498.

Copy link

@tapted tapted left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for picking this up so quickly. And sorry for the lag - #timezones, plus I had to learn how to use github codereview haha.

src/darker/tests/test_linting.py Outdated Show resolved Hide resolved
Comment on lines 587 to 597
def __init__(
self,
args: List[str],
stdout: int,
encoding: str,
cwd: Path,
env: Dict[str, str],
):
super().__init__(
args, stdout=stdout, stderr=PIPE, encoding=encoding, cwd=cwd, env=env
)
Copy link

Choose a reason for hiding this comment

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

Not sure if it fits well with your prevailing style (or typechecking - I'm still learning how to bend mypy to my will :p), but I think you could do

Suggested change
def __init__(
self,
args: List[str],
stdout: int,
encoding: str,
cwd: Path,
env: Dict[str, str],
):
super().__init__(
args, stdout=stdout, stderr=PIPE, encoding=encoding, cwd=cwd, env=env
)
def __init__(self, args: List[str], **kwargs):
kwargs['stderr'] = PIPE
super().__init__(args, **kwargs)

(this works for Popen because it explicitly declares its trailing parameters to be keyword-only)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I originally had *args, **kwargs but Mypy wasn't happy for it. The options were to omit types and do a #type: ignore, or to separate and annotate the keyword arguments.

I guess for unit tests and such a minimal helper skipping type checking would be just fine. Would you prefer that?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Actually this was trivial to do. The class is now much more concise.



def test_get_messages_from_linters_for_baseline_no_mypy_errors(git_repo, capsys):
"""Test for `linting._get_messages_from_linters_for_baseline`"""
Copy link

Choose a reason for hiding this comment

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

nit: Should it be something like

Suggested change
"""Test for `linting._get_messages_from_linters_for_baseline`"""
"""Ensure mypy does not fail early when __init__.py is at the repo root"""

in our codebase we sometimes also mention the bug ID (#498) in a comment somewhere. E.g.,

# Regression test for #498.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I admit my unit test docstring convention isn't always too useful. I fixed that according to your suggestion.

Copy link

Choose a reason for hiding this comment

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

not sure I understand what this file is for (could it be a stray untracked file that got added?)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Removed, accidental. Was related to getting Python 3.12.dev running on my NixOS laptop.

@akaihola akaihola force-pushed the mypy-baseline-fix branch from 5a2d820 to eb41d5c Compare May 31, 2023 07:33
@akaihola akaihola force-pushed the mypy-baseline-fix branch from eb41d5c to abc9515 Compare May 31, 2023 07:37
akaihola added 3 commits May 31, 2023 10:48
Asserts that Mypy doesn't output anything on stderr. This currently
fails when running Mypy for the baseline, because the temporary
directory name contains a hyphen which makes it an invalid Python
package name.
Use the original repository root directory name. This way Mypy behaves
similarly to the original working directory and doesn't fail when
there's an `__init__.py` in the root.
@akaihola akaihola force-pushed the mypy-baseline-fix branch from abc9515 to 056ff47 Compare May 31, 2023 07:49
@akaihola
Copy link
Owner Author

Phew, I think I've now dealt with Python<3.9, Mypy, Pylint and Bandit and this can be merged.

I rebased the modifications into the original commits.

Gotta say, despite the trouble, I still do value highly linters, good test coverage and especially code reviews. Thanks @tapted!

@akaihola akaihola merged commit 0fb2501 into master May 31, 2023
@akaihola akaihola deleted the mypy-baseline-fix branch May 31, 2023 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
2 participants