-
Notifications
You must be signed in to change notification settings - Fork 12
add mypy support #106
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
sneakers-the-rat
wants to merge
5
commits into
pyOpenSci:main
Choose a base branch
from
sneakers-the-rat:add-mypy
base: main
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
add mypy support #106
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
006659d
add mypy support
sneakers-the-rat 932f77c
add mypy support
sneakers-the-rat 77f4113
Merge branch 'add-mypy' of https://github.com/sneakers-the-rat/pyos-p…
sneakers-the-rat 976f8ba
does windows shell care about double quotes lol
sneakers-the-rat d5c83e0
windows istg
sneakers-the-rat 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 |
---|---|---|
|
@@ -27,7 +27,7 @@ style = [ | |
"ruff", | ||
] | ||
{% endif %} | ||
{%- if use_types -%} | ||
{%- if use_mypy -%} | ||
types = [ | ||
"mypy", | ||
] | ||
|
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
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 |
---|---|---|
|
@@ -22,8 +22,10 @@ | |
import os | ||
import subprocess | ||
import sys | ||
import venv | ||
from datetime import datetime, timezone | ||
from pathlib import Path | ||
from textwrap import dedent | ||
from typing import Callable | ||
|
||
import pytest | ||
|
@@ -196,7 +198,7 @@ def test_docs_build(documentation: str, generated: Callable[..., Path], use_hatc | |
) | ||
run_command("sphinx-apidoc -o docs/api src/alien_clones", project) | ||
# prepend pythonpath so we don't have to actually install here... | ||
run_command(f"PYTHONPATH={str(project/'src')} sphinx-build -W -b html docs docs/_build", project) | ||
run_command(f"PYTHONPATH={project/'src'!s} sphinx-build -W -b html docs docs/_build", project) | ||
|
||
run_command("pre-commit run --all-files -v check-readthedocs", project) | ||
|
||
|
@@ -229,7 +231,7 @@ def test_non_hatch_deps( | |
project = generated( | ||
use_hatch_envs=False, | ||
use_lint=True, | ||
use_types=True, | ||
use_mypy=True, | ||
use_test=True, | ||
use_git=False, | ||
documentation=documentation, | ||
|
@@ -254,3 +256,50 @@ def test_non_hatch_deps( | |
if documentation != "no": | ||
assert "docs" in optional_deps | ||
assert any(dep.startswith(documentation) for dep in optional_deps["docs"]) | ||
|
||
@pytest.mark.parametrize("use_hatch_envs", [True, False]) | ||
@pytest.mark.parametrize("valid", [True, False]) | ||
def test_mypy(generated: Callable[..., Path], use_hatch_envs: bool, valid: bool, tmp_path: Path): | ||
"""Mypy type checking works out of the box.""" | ||
if valid: | ||
module = dedent(""" | ||
def f1(value: int, other: int = 2) -> int: | ||
return value * other | ||
|
||
def f2(value: int) -> float: | ||
return f1(value, 5) / 2 | ||
""") | ||
else: | ||
module = dedent(""" | ||
def f1(value: int, other: int = 2) -> int: | ||
return value * other | ||
|
||
def f2(value: str) -> str: | ||
return f1(value, 5) / 2 | ||
""") | ||
|
||
root = generated(use_hatch_envs=use_hatch_envs, use_mypy=True) | ||
pkg_path = root / "src" / "alien_clones" | ||
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. Can the name be gotten from the generated project somehow? Would be better than hard-coding it here. |
||
module_path = pkg_path / "typechecking.py" | ||
with module_path.open("w") as f: | ||
f.write(module) | ||
|
||
if use_hatch_envs: | ||
command = "hatch run types:check" | ||
else: | ||
venv_path = tmp_path / ".venv" | ||
if sys.platform == "win32": | ||
mypy_path = venv_path / "Scripts" / "mypy.exe" | ||
python_path = venv_path / "Scripts" / "python.exe" | ||
else: | ||
mypy_path = venv_path / "bin" / "mypy" | ||
python_path = venv_path / "bin" / "python" | ||
venv.EnvBuilder(with_pip=True).create(venv_path) | ||
run_command(f'{python_path!s} -m pip install -e ".[types]"', root) | ||
command = f"{mypy_path!s} {pkg_path!s}" | ||
|
||
if valid: | ||
run_command(command, root) | ||
else: | ||
with pytest.raises(subprocess.CalledProcessError): | ||
run_command(command, root) |
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.
I originally went with
use_types
, as folks might want to run pyright or another alternative to mypy. It's fair to commit to mypy, though, especially as this is targeted more at beginners.