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

feat: mypyc [wip] #57

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ jobs:

- name: install
run: python -m pip install -e .[test]
env:
HATCH_BUILD_HOOKS_ENABLE: "1"

- name: Run benchmarks
uses: CodSpeedHQ/action@v2
Expand Down
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
.PHONY: build build-trace check clean cleanc benchmark-all benchmark-compare
.PHONY: build check clean benchmark-all benchmark-compare

build:
python setup.py build_ext --inplace
cleanc

build-trace:
python setup.py build_ext --force --inplace --define CYTHON_TRACE
cleanc
HATCH_BUILD_HOOKS_ENABLE=1 pip install -e .

check:
pre-commit run --all-files

cleanc:
rm -f src/in_n_out/*.c

clean:
rm -rf `find . -name __pycache__`
rm -f `find . -type f -name '*.py[co]' `
Expand Down
39 changes: 23 additions & 16 deletions asv.conf.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
{
"version": 1,
"project": "in-n-out",
"project_url": "https://github.com/pyapp-kit/in-n-out",
"repo": ".",
"branches": ["main"],
"dvcs": "git",
"environment_type": "conda",
"conda_channels": ["conda-forge"],
"install_timeout": 600,
"show_commit_url": "https://github.com/pyapp-kit/in-n-out/commit/",
"pythons": ["3.10"],
"build_command": ["pip install build", "python -m build --wheel -o {build_cache_dir} {build_dir}"],
"env_dir": "/tmp/.asv/env",
"results_dir": ".asv/results",
"html_dir": ".asv/html"
}
"version": 1,
"project": "in-n-out",
"project_url": "https://github.com/pyapp-kit/in-n-out",
"repo": ".",
"branches": ["main"],
"dvcs": "git",
"environment_type": "conda",
"install_timeout": 600,
"show_commit_url": "https://github.com/pyapp-kit/in-n-out/commit/",
"pythons": ["3.10"],
"build_command": [
"python -m pip install build",
"python -m build --wheel -o {build_cache_dir} {build_dir}"
],
"matrix": {
"env": {
"HATCH_BUILD_HOOKS_ENABLE": "1"
}
},
"env_dir": "/tmp/.asv/env",
"results_dir": ".asv/results",
"html_dir": ".asv/html"
}
25 changes: 22 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://peps.python.org/pep-0517/
[build-system]
requires = ["hatchling", "hatch-vcs"]
requires = ["hatchling>=1.8.0", "hatch-vcs"]
build-backend = "hatchling.build"

# https://peps.python.org/pep-0621/
Expand Down Expand Up @@ -30,12 +30,13 @@ dependencies = []
source = "vcs"

[tool.hatch.build.targets.sdist]
include = ["/src", "/tests"]
include = ["src", "tests", "CHANGELOG.md"]

[tool.hatch.build.targets.wheel]
only-include = ["src"]
sources = ["src"]


# extras
# https://peps.python.org/pep-0621/#dependencies-optional-dependencies
[project.optional-dependencies]
Expand All @@ -62,7 +63,21 @@ homepage = "https://github.com/pyapp-kit/in-n-out"
repository = "https://github.com/pyapp-kit/in-n-out"
documentations = "https://ino.rtfd.io"

# https://docs.astral.sh/ruff

[tool.hatch.build.targets.wheel.hooks.mypyc]
mypy-args = ["--ignore-missing-imports"]
enable-by-default = false
require-runtime-dependencies = true
dependencies = ["hatch-mypyc>=0.13.0", "mypy>=0.991"]
include = [
'src/in_n_out/_store.py',
'src/in_n_out/_global.py',
'src/in_n_out/_type_resolution.py',
'src/in_n_out/_util.py',
]


# https://github.com/charliermarsh/ruff
[tool.ruff]
line-length = 88
src = ["src", "tests"]
Expand Down Expand Up @@ -127,6 +142,10 @@ disallow_any_generics = false
show_error_codes = true
pretty = true

[[tool.mypy.overrides]]
module = ["cython", "toolz"]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = ["tests.*"]
disallow_untyped_defs = false
Expand Down
20 changes: 12 additions & 8 deletions src/in_n_out/_global.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import contextlib
from textwrap import indent
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, overload
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, TypeVar, overload

from ._store import InjectionContext, Store

Expand All @@ -25,20 +26,23 @@
global store is used.
"""
_STORE_PARAM = indent(_STORE_PARAM.strip(), " ")
F = TypeVar("F", bound=Callable[..., Any])


def _add_store_to_doc(func: T) -> T:
def _add_store_to_doc(func: F) -> F:
new_doc: list[str] = []

store_doc: str = getattr(Store, func.__name__).__doc__ # type: ignore
name: str = func.__name__
store_doc: str = getattr(Store, name).__doc__ or ""
for n, line in enumerate(store_doc.splitlines()):
if line.lstrip().startswith("Returns"):
new_doc.insert(n - 1, _STORE_PARAM)

# TODO: use re.sub instead
new_doc.append(line.replace(" store.", " ").replace("@store.", "@"))

func.__doc__ = "\n".join(new_doc)
with contextlib.suppress(AttributeError): # when compiled
func.__doc__ = "\n".join(new_doc)
return func


Expand Down Expand Up @@ -223,7 +227,7 @@ def process(

@overload
def inject(
func: Callable[P, R],
func: Callable[..., R],
*,
providers: bool = True,
processors: bool = False,
Expand Down Expand Up @@ -255,7 +259,7 @@ def inject(

@_add_store_to_doc
def inject(
func: Callable[P, R] | None = None,
func: Callable[..., R] | None = None,
*,
providers: bool = True,
processors: bool = False,
Expand All @@ -282,7 +286,7 @@ def inject(

@overload
def inject_processors(
func: Callable[P, R],
func: Callable[..., R],
*,
hint: object | type[T] | None = None,
first_processor_only: bool = False,
Expand All @@ -304,7 +308,7 @@ def inject_processors(

@_add_store_to_doc
def inject_processors(
func: Callable[P, R] | None = None,
func: Callable[..., R] | None = None,
*,
hint: object | type[T] | None = None,
first_processor_only: bool = False,
Expand Down
Loading
Loading