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

basedmypy 🎉 #51

Merged
merged 10 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:
cancel-in-progress: true

env:
VERSION_UV: '0.4.10'
VERSION_UV: '0.4.11'

jobs:
lint:
Expand Down Expand Up @@ -52,6 +52,9 @@ jobs:
with:
plugins: sp-repo-review

- name: basedmypy
run: uv run mypy

- name: basedpyright
run: uv run basedpyright

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ dist/
site/

# Cache
.mypy_cache/
.pytest_cache/
.ruff_cache/
.tox/

# Environments
.env
Expand Down
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ci:
- poetry-lock
- codespell
- ruff
- basedmypy
- basedpyright
- basedpyright-verifytypes

Expand Down Expand Up @@ -54,13 +55,19 @@ repos:
- tomli

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
rev: v0.6.5
hooks:
- id: ruff
args: [--fix, --show-fixes]

- repo: local
hooks:
- id: basedmypy
name: basedmypy
entry: uv run mypy
language: system
types_or: [python, pyi]

- id: basedpyright
name: basedpyright
entry: uv run basedpyright
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
src="https://img.shields.io/badge/pre--commit-enabled-orange?logo=pre-commit"
/>
</a>
<!-- <a href="https://github.com/KotlinIsland/basedmypy">
<a href="https://github.com/KotlinIsland/basedmypy">
<img
alt="mainpy - basedmypy"
src="https://img.shields.io/badge/basedmypy-checked-fd9002"
/>
</a> -->
</a>
<a href="https://detachhead.github.io/basedpyright">
<img
alt="mainpy - basedpyright"
Expand Down
17 changes: 10 additions & 7 deletions mainpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def _infer_debug() -> bool:
if '--debug' in sys.argv[1:]:
return True

env_debug = os.environ.get('DEBUG', '0')
env_debug_str = os.environ.get('DEBUG', '0')
try:
env_debug = int(env_debug)
env_debug = int(env_debug_str)
except ValueError as e:
errmsg = f'Invalid value for `DEBUG` env var: {env_debug!r}'
errmsg = f'Invalid value for `DEBUG` env var: {env_debug_str!r}'
raise OSError(errmsg) from e

return bool(env_debug)
Expand Down Expand Up @@ -70,7 +70,10 @@ def callback(self, /) -> Callable[[], _R_co]: ...
def __call__(self, /) -> _R_co: ...


def _is_click_cmd(func: _F) -> TypeGuard[_HasCallbackFunction[_F]]:
def _is_click_cmd(
func: _HasCallbackFunction[object] | _F,
/,
) -> TypeGuard[_HasCallbackFunction[_F]]:
KotlinIsland marked this conversation as resolved.
Show resolved Hide resolved
return func.__module__ == 'click.core' and hasattr(func, 'callback')


Expand All @@ -84,7 +87,7 @@ def _unwrap_click(func: _HasCallbackFunction[object] | _F, /) -> _F | object:
return func


def _is_main_func(func: Callable[..., object], /) -> bool:
def _is_main_func(func: Callable[[], object], /) -> bool:
return _unwrap_click(func).__module__ == '__main__'


Expand Down Expand Up @@ -163,15 +166,15 @@ def _main(_func: _F, /) -> _F:

return _main

if not callable(func):
if not callable(func): # type: ignore[redundant-expr]
errmsg = f'expected a callable, got {type(func)}'
raise TypeError(errmsg)

if not _is_main_func(func):
import inspect

frame = inspect.currentframe()
if not frame or frame.f_globals.get('__name__') != '__main__':
if not frame or frame.f_globals.get('__name__') != '__main__': # type: ignore[no-any-expr]
return func

if debug is None:
Expand Down
25 changes: 25 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dev-dependencies = [
"typing_extensions>=4.12.2",
"uvloop; sys_platform != 'win32'",

"basedmypy>=2.6.0,<3",
"basedpyright>=1.17.5,<2",
"codespell>=2.3.0,<3",
"pytest>=8.3.3,<9",
Expand Down Expand Up @@ -105,6 +106,30 @@ venvPath = "."
reportUnreachable = false # unavoidable with `sys.version_info` conditionals


[tool.mypy]
python_version = "3.9"
modules = ["mainpy", "tests"]
exclude = ["^.venv/.*"]
jorenham marked this conversation as resolved.
Show resolved Hide resolved
strict = true

disallow_any_generics = true
disallow_untyped_calls = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_unreachable = false
# based
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_expr = true
disallow_subclassing_any = true
jorenham marked this conversation as resolved.
Show resolved Hide resolved


[[tool.mypy.overrides]]
module = ["tests.*"]
disallow_any_decorated = false # blame pytest

[tool.repo-review]
ignore = [
"PY004", # README.md >> docs/
Expand Down
58 changes: 58 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.