Skip to content

Commit

Permalink
drop 3.7, add 3.12, bump pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
rsokl committed Mar 23, 2024
1 parent e074164 commit 8c81703
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 107 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tox_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
max-parallel: 5
matrix:
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]
fail-fast: false

steps:
Expand All @@ -39,10 +39,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
12 changes: 5 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
repos:
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 24.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
args: [--ignore, "F811,D1,D205,D209,D213,D400,D401,D999,D202,E203,E501,W503,E721,F403,F405",
--exclude, "versioneer.py,docs/*,tests/annotations/*, tests/test_py310.py"]
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
2 changes: 1 addition & 1 deletion deps/requirements-pyright.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pyright==1.1.309
pyright==1.1.355
23 changes: 12 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ build-backend = "setuptools.build_meta"
[project]
name = "phantom_tensors"
dynamic = ["version"]
description = "Configurable, reproducible, and scalable workflows in Python, via Hydra"
description = "Tensor-like types – with variadic shapes – that support both static and runtime type checking, and convenient parsing."
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
dependencies = ["typing-extensions >= 4.1.0"]
license = { text = "MIT" }
keywords = [
Expand All @@ -37,11 +37,11 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Programming Language :: Python :: 3 :: Only",
]
Expand Down Expand Up @@ -121,15 +121,15 @@ skip = 'docs/build/*'
legacy_tox_ini = """
[tox]
isolated_build = True
envlist = py37, py38, py39, py310, py311
envlist = py38, py39, py310, py311, py312
[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310
3.11: py311
3.12: py312
[testenv]
description = Runs test suite parallelized in the specified python enviornment and
Expand Down Expand Up @@ -162,7 +162,7 @@ commands = pytest --cov-report term-missing --cov-config=pyproject.toml --cov-fa
description = Runs test suite against optional 3rd party packages that phantom-tensors
provides specialized support for.
install_command = pip install --upgrade --upgrade-strategy eager {opts} {packages}
basepython = python3.9
basepython = python3.11
deps = {[testenv]deps}
beartype
torch
Expand All @@ -183,10 +183,11 @@ deps =
numpy
phantom-types
commands = pyright --lib tests/ src/ --pythonversion=3.8
pyright --lib tests/ src/ --pythonversion=3.9
pyright --lib tests/ src/ --pythonversion=3.10
pyright --lib tests/ src/ --pythonversion=3.11
commands = pyright tests/ src/ --pythonversion=3.8
pyright tests/ src/ --pythonversion=3.9
pyright tests/ src/ --pythonversion=3.10
pyright tests/ src/ --pythonversion=3.11
pyright tests/ src/ --pythonversion=3.12
pyright --ignoreexternal --verifytypes phantom_tensors
Expand All @@ -209,7 +210,7 @@ commands =
description = Ensures that source materials code and docs and test suite adhere to
formatting and code-quality standards.
skip_install=true
basepython=python3.9
basepython=python3.11
deps=black
isort
flake8
Expand Down
4 changes: 2 additions & 2 deletions src/phantom_tensors/_internals/dim_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def check(shape_type: Tuple[ShapeDimType, ...], shape: Tuple[int, ...]) -> bool:
f"int or subclass of int. shape-type {shape_type} contains a "
f"NewType of supertype {_supertype}"
)
validators[dim_symbol] = lambda x, sp=_supertype: isinstance(x, sp) # type: ignore
validators[dim_symbol] = lambda x, sp=_supertype: isinstance(x, sp)
_match_list.append(CURRENT_INDEX)
del _supertype
elif isinstance(dim_symbol, TypeVar):
Expand All @@ -131,7 +131,7 @@ def check(shape_type: Tuple[ShapeDimType, ...], shape: Tuple[int, ...]) -> bool:
del _expected_literals

elif isinstance(dim_symbol, type) and issubclass(dim_symbol, int):
validators[dim_symbol] = lambda x, type_=dim_symbol: isinstance(x, type_) # type: ignore
validators[dim_symbol] = lambda x, type_=dim_symbol: isinstance(x, type_)
_validate_list.append(CURRENT_INDEX)
else:
raise TypeError(
Expand Down
29 changes: 10 additions & 19 deletions src/phantom_tensors/_internals/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class _Phantom(Protocol):

class HasShape(Protocol):
@property
def shape(self) -> Any:
...
def shape(self) -> Any: ...


TupleInt: TypeAlias = Tuple[int, ...]
Expand Down Expand Up @@ -127,8 +126,7 @@ def __call__(
__d: Tuple[HasShape, Type[S4]],
__e: Tuple[HasShape, Type[S5]],
__f: Tuple[HasShape, Type[S6]],
) -> Tuple[S1, S2, S3, S4, S5, S6]:
...
) -> Tuple[S1, S2, S3, S4, S5, S6]: ...

@overload
def __call__(
Expand All @@ -138,8 +136,7 @@ def __call__(
__c: Tuple[HasShape, Type[S3]],
__d: Tuple[HasShape, Type[S4]],
__e: Tuple[HasShape, Type[S5]],
) -> Tuple[S1, S2, S3, S4, S5]:
...
) -> Tuple[S1, S2, S3, S4, S5]: ...

@overload
def __call__(
Expand All @@ -148,44 +145,38 @@ def __call__(
__b: Tuple[HasShape, Type[S2]],
__c: Tuple[HasShape, Type[S3]],
__d: Tuple[HasShape, Type[S4]],
) -> Tuple[S1, S2, S3, S4]:
...
) -> Tuple[S1, S2, S3, S4]: ...

@overload
def __call__(
self,
__a: Tuple[HasShape, Type[S1]],
__b: Tuple[HasShape, Type[S2]],
__c: Tuple[HasShape, Type[S3]],
) -> Tuple[S1, S2, S3]:
...
) -> Tuple[S1, S2, S3]: ...

@overload
def __call__(
self,
__a: HasShape,
__b: Type[S1],
) -> S1:
...
) -> S1: ...

@overload
def __call__(
self,
__a: Tuple[HasShape, Type[S1]],
__b: Tuple[HasShape, Type[S2]],
) -> Tuple[S1, S2]:
...
) -> Tuple[S1, S2]: ...

@overload
def __call__(self, __a: Tuple[HasShape, Type[S1]]) -> S1:
...
def __call__(self, __a: Tuple[HasShape, Type[S1]]) -> S1: ...

@overload
def __call__(
self,
*tensor_type_pairs: Tuple[HasShape, Type[HasShape]] | HasShape | Type[HasShape],
) -> HasShape | Tuple[HasShape, ...]:
...
) -> HasShape | Tuple[HasShape, ...]: ...

@dim_binding_scope
def __call__(
Expand All @@ -198,7 +189,7 @@ def __call__(
tensor_type_pairs = (tensor_type_pairs,) # type: ignore

pairs = cast(
Tuple[Tuple[HasShape, Type[HasShape]], ...], _to_tuple(tensor_type_pairs)
Tuple[Tuple[HasShape, Type[HasShape]], ...], _to_tuple(tensor_type_pairs) # type: ignore
)

out: List[HasShape] = []
Expand Down
10 changes: 4 additions & 6 deletions src/phantom_tensors/_internals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@ class NewTypeLike(Protocol):
__name__: str
__supertype__: Type[Any]

def __call__(self, x: Any) -> int:
...
def __call__(self, x: Any) -> int: ...


class NewTypeInt(Protocol):
__name__: str
__supertype__: Type[int]

def __call__(self, x: Any) -> int:
...
def __call__(self, x: Any) -> int: ...


class UnpackLike(Protocol):
_inst: Literal[True]
_name: Literal[None]
__origin__: Type[Any] = Unpack
__origin__: Type[Any] = Unpack # type: ignore
__args__: Tuple[TypeVarTuple]
__parameters__: Tuple[TypeVarTuple]
__module__: str
Expand All @@ -44,7 +42,7 @@ class UnpackLike(Protocol):
class LiteralLike(Protocol):
_inst: Literal[True]
_name: Literal[None]
__origin__: Type[Any] = Literal
__origin__: Type[Any] = Literal # type: ignore
__args__: Tuple[Any, ...]
__parameters__: Tuple[()]
__module__: str
Expand Down
6 changes: 2 additions & 4 deletions src/phantom_tensors/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

@runtime_checkable
class SupportsArray(Protocol[_te.Unpack[Shape]]):
def __array__(self) -> Any:
...
def __array__(self) -> Any: ...

@property
def shape(self) -> Tuple[_te.Unpack[Shape]]:
...
def shape(self) -> Tuple[_te.Unpack[Shape]]: ...
3 changes: 1 addition & 2 deletions src/phantom_tensors/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ def shape(self) -> Tuple[_te.Unpack[Shape]]: # type: ignore
...

@shape.setter
def shape(self, value: Union[SupportsIndex, Sequence[SupportsIndex]]) -> None:
...
def shape(self, value: Union[SupportsIndex, Sequence[SupportsIndex]]) -> None: ...
3 changes: 1 addition & 2 deletions src/phantom_tensors/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
Shape = _te.TypeVarTuple("Shape")


class _NewMeta(CustomInstanceCheck, type(_Tensor)):
...
class _NewMeta(CustomInstanceCheck, type(_Tensor)): ...


class Tensor(Generic[_te.Unpack[Shape]], _Tensor):
Expand Down
23 changes: 8 additions & 15 deletions tests/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,11 @@ def check_readme2():
from phantom_tensors.alphabet import A, B # these are just NewType(..., int) types
from phantom_tensors.numpy import NDArray

def func_on_2d(x: NDArray[Any, Any]):
...
def func_on_2d(x: NDArray[Any, Any]): ...

def func_on_3d(x: NDArray[Any, Any, Any]):
...
def func_on_3d(x: NDArray[Any, Any, Any]): ...

def func_on_any_arr(x: np.ndarray[Any, Any]):
...
def func_on_any_arr(x: np.ndarray[Any, Any]): ...

if sys.version_info < (3, 8):
return
Expand Down Expand Up @@ -203,11 +200,9 @@ def check_readme4():
assert_type(t2, Tensor[B])
assert_type(w, Tensor[A])

def vanilla_numpy(x: np.ndarray[Any, Any]):
...
def vanilla_numpy(x: np.ndarray[Any, Any]): ...

def vanilla_torch(x: tr.Tensor):
...
def vanilla_torch(x: tr.Tensor): ...

vanilla_numpy(arr) # type checker: OK
vanilla_torch(t1) # type checker: OK
Expand All @@ -218,13 +213,12 @@ def check_phantom_example():
from typing import Any

import torch as tr
from phantom import Phantom

from phantom import Phantom
from phantom_tensors import parse
from phantom_tensors.torch import Tensor

class EvenOnly(int, Phantom[Any], predicate=lambda x: x % 2 == 0):
...
class EvenOnly(int, Phantom[Any], predicate=lambda x: x % 2 == 0): ...

assert_type(parse(tr.ones(1, 0), Tensor[int, EvenOnly]), Tensor[int, EvenOnly])
assert_type(parse(tr.ones(1, 2), Tensor[int, EvenOnly]), Tensor[int, EvenOnly])
Expand Down Expand Up @@ -255,8 +249,7 @@ def matrix_multiply(x: Tensor[A, B], y: Tensor[B, C]) -> Tensor[A, C]:
return parse(tr.rand(a, c), Tensor[A, C])

@beartype
def needs_vector(x: Tensor[Any]):
...
def needs_vector(x: Tensor[Any]): ...

x, y = parse(
(tr.rand(3, 4), Tensor[A, B]),
Expand Down
3 changes: 1 addition & 2 deletions tests/arrlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def __init__(self, shape: Tuple[int, ...]) -> None:
def shape(self) -> Tuple[int, ...]:
return self._shape

def __array__(self) -> Any:
...
def __array__(self) -> Any: ...


def arr(*shape: int) -> ImplementsArray:
Expand Down
Loading

0 comments on commit 8c81703

Please sign in to comment.