8000 Added get_type_hints for intersection, and first test with reveal typ… · CarliJoy/intersection_examples@f9c9c0a · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit f9c9c0a

Browse files
committed
Added get_type_hints for intersection, and first test with reveal type producing
same as based mypy
1 parent d1fa22f commit f9c9c0a

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

intersection_examples/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1-
__all__ = ["Intersection"]
1+
__all__ = ["Intersection", "get_type_hints"]
2+
3+
from typing import Any, get_args, get_origin
4+
from typing import get_type_hints as get_type_hints_old
25

36
from basedtyping import Intersection
7+
8+
9+
def get_type_hints(
10+
obj: Any,
11+
globalns: Any | None = None,
12+
localns: Any | None = None,
13+
include_extras: bool = False,
14+
) -> dict[str, Any]:
15+
if get_origin(obj) == Intersection:
16+
args = get_args(obj)
17+
new_type_hints = {}
18+
for arg in args:
19+
new_type_hints.update(get_type_hints(arg))
20+
return new_type_hints
21+
else:
22+
return get_type_hints_old(
23+
obj, globalns=globalns, localns=localns, include_extras=include_extras
24+
)

intersection_examples/py.typed

Whitespace-only changes.

tests/mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
plugins = typing_protocol_intersection.mypy_plugin
3+
disable_error_code = no-any-explicit, no-any-expr

tests/mypy_test_basic.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Protocol, get_type_hints, overload, reveal_type, runtime_checkable
1+
from typing import Any, Protocol, cast, reveal_type
22

33
import pytest
4-
from basedtyping import Intersection
4+
5+
from intersection_examples import Intersection
56

67

78
@pytest.mark.mypy_testing
@@ -14,13 +15,9 @@ class B(float):
1415

1516
AB = Intersection[A, B]
1617

17-
@runtime_checkable
1818
class AB_proto(Protocol):
1919
a: str
2020
b: str
2121

22-
# print(get_type_hints(AB_proto))
23-
# print(get_type_hints(AB))
24-
2522
print(reveal_type(AB)) # R: object
26-
print(reveal_type(AB_proto)) # R: def () -> mypy_test_basic.AB_proto@19
23+
print(reveal_type(AB_proto)) # R: def () -> mypy_test_basic.AB_proto@16

tests/test_type_hints.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Protocol
2+
3+
from intersection_examples import Intersection, get_type_hints
4+
83E3 5+
6+
def test_class_intersect():
7+
class A(str):
8+
a: str
9+
10+
class B(float):
11+
b: str
12+
13+
AB = Intersection[A, B]
14+
15+
class AB_proto(Protocol):
16+
a: str
17+
b: str
18+
19+
assert get_type_hints(AB) == get_type_hints(AB_proto)

0 commit comments

Comments
 (0)
0