Skip to content

Commit 27ca663

Browse files
authored
Make typemap_extensions the canonical import for users (#78)
The point here is to make sure that *clients* import the `typemap.typing` stuff under a different name that `type_eval` internals do, so that `typemap.type_eval` internals can still be typechecked against the real `typemap.typing` while client code will import `typemap_extensions` and will be able to see the proper mypy/typeshed magic for it. If we want to keep things under `typemap`, we could leave the public version as `typemap.typing` and make the internal module `typemap._typing_internals`. Thoughts? Also do some fixups so that qblike_2 and fastapilike_2 basically pass typechecking.
1 parent 14537c2 commit 27ca663

19 files changed

Lines changed: 68 additions & 25 deletions

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ See [pep.rst](pep.rst) for the PEP draft and [design-qs.rst](design-qs.rst) for
88
2. `$ cd typemap`
99
3. `$ uv sync`
1010
4. `$ uv run pytest`
11+
12+
## Running the typechecker
13+
14+
If you have https://github.com/msullivan/mypy/tree/typemap active in a
15+
venv, you can run it against at least some of the tests with
16+
invocations like:
17+
`mypy --python-version=3.14 tests/test_qblike_2.py`
18+
19+
Not all of them run cleanly yet though.

pep.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,8 @@ as a literal type--all of these mechanisms lean very heavily on literal types.
10051005
for c in typing.Iter[typing.Attrs[K]]
10061006
]
10071007
]
1008-
]: ...
1008+
]:
1009+
raise NotImplementedError
10091010

10101011
ConvertField is our first type helper, and it is a conditional type
10111012
alias, which decides between two types based on a (limited)
@@ -1035,7 +1036,7 @@ grabs the argument to a ``Pointer``).
10351036

10361037
::
10371038

1038-
type PointerArg[T: Pointer] = typing.GetArg[T, Pointer, Literal[0]]
1039+
type PointerArg[T] = typing.GetArg[T, Pointer, Literal[0]]
10391040

10401041
``AdjustLink`` sticks a ``list`` around ``MultiLink``, using features
10411042
we've discussed already.

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ readme = "README.md"
66
requires-python = ">=3.14"
77
dependencies = []
88

9+
[tool.setuptools.packages.find]
10+
include = ["typemap", "typemap_extensions"]
11+
912
[dependency-groups]
1013
test = [
1114
"pytest>=7.0",
@@ -68,7 +71,7 @@ extend-ignore = [
6871
[tool.ruff]
6972
line-length = 80
7073
indent-width = 4
71-
include = ["pyproject.toml", "typemap/**/*.py", "tests/**/*.py"]
74+
include = ["pyproject.toml", "typemap/**/*.py", "typemap_extensions/**/*.py", "tests/**/*.py"]
7275

7376
[tool.ruff.format]
7477
quote-style = "preserve"

tests/format_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def format_meth(name, meth):
2525
code += f" {attr_name}: {attr_type_s}{eq}\n"
2626

2727
for name, attr in cls.__dict__.items():
28-
if attr is typing._no_init_or_replace_init:
28+
if attr is typing._no_init_or_replace_init: # type: ignore[attr-defined]
2929
continue
3030
if isinstance(attr, classmethod):
31-
attr = inspect.unwrap(attr)
31+
attr = inspect.unwrap(attr) # type: ignore[arg-type]
3232
code += f" @classmethod\n"
3333
elif isinstance(attr, staticmethod):
3434
attr = inspect.unwrap(attr)

tests/test_astlike_1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import typing
33

44
from typemap.type_eval import eval_call_with_types, eval_typing, TypeMapError
5-
from typemap.typing import (
5+
from typemap.typing import _BoolLiteral
6+
7+
from typemap_extensions import (
68
Attrs,
79
BaseTypedDict,
810
Bool,
@@ -15,7 +17,6 @@
1517
Member,
1618
NewProtocol,
1719
RaiseError,
18-
_BoolLiteral,
1920
)
2021

2122

tests/test_call.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Unpack
44

55
from typemap.type_eval import eval_call
6-
from typemap.typing import (
6+
from typemap_extensions import (
77
Attrs,
88
BaseTypedDict,
99
NewProtocol,

tests/test_eval_call_with_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Callable, Generic, Literal, Self, TypeVar
44

55
from typemap.type_eval import eval_call_with_types
6-
from typemap.typing import (
6+
from typemap_extensions import (
77
GenericCallable,
88
GetArg,
99
GetName,

tests/test_fastapilike_1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Annotated, Callable, Literal, Union, Self
66

77
from typemap.type_eval import eval_typing
8-
from typemap.typing import (
8+
from typemap_extensions import (
99
NewProtocol,
1010
Iter,
1111
Attrs,

tests/test_fastapilike_2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
TypedDict,
77
Never,
88
Self,
9+
TYPE_CHECKING,
910
)
1011

11-
from typemap import typing
12+
import typemap_extensions as typing
1213

1314

1415
class FieldArgs(TypedDict, total=False):
@@ -25,7 +26,7 @@ class Field[T: FieldArgs](typing.InitField[T]):
2526
####
2627

2728
# TODO: Should this go into the stdlib?
28-
type GetFieldItem[T: typing.InitField, K] = typing.GetMemberType[
29+
type GetFieldItem[T, K] = typing.GetMemberType[
2930
typing.GetArg[T, typing.InitField, Literal[0]], K
3031
]
3132

@@ -207,6 +208,11 @@ class Hero:
207208
secret_name: str = Field(hidden=True)
208209

209210

211+
# Quick reveal_type test for running mypy against this
212+
if TYPE_CHECKING:
213+
pubhero: Public[Hero]
214+
reveal_type(pubhero) # noqa
215+
210216
#######
211217

212218
import textwrap

tests/test_nplike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Literal
22

3-
from typemap import typing
3+
import typemap_extensions as typing
44

55
import pytest
66

0 commit comments

Comments
 (0)