-
Notifications
You must be signed in to change notification settings - Fork 56
Refactor typings #62
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
Merged
Merged
Refactor typings #62
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ __pycache__/ | |
/.pytest_cache | ||
/.coverage | ||
/.mypy_cache | ||
/.venv* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
recursive-include tests *.py | ||
recursive-include immutables *.py *.c *.h *.pyi | ||
include LICENSE* NOTICE README.rst bench.png | ||
include immutables/py.typed | ||
include mypy.ini immutables/py.typed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,73 @@ | ||
from typing import Any | ||
from typing import Dict | ||
from typing import Generic | ||
from typing import Hashable | ||
from typing import Iterable | ||
from typing import Iterator | ||
from typing import Mapping | ||
from typing import MutableMapping | ||
from typing import NoReturn | ||
from typing import overload | ||
from typing import Optional | ||
from typing import Tuple | ||
from typing import Type | ||
from typing import TypeVar | ||
from typing import Union | ||
from typing import overload | ||
|
||
|
||
K = TypeVar('K', bound=Hashable) | ||
V = TypeVar('V', bound=Any) | ||
D = TypeVar('D', bound=Any) | ||
|
||
|
||
class BitmapNode: ... | ||
|
||
|
||
class MapKeys(Generic[K]): | ||
def __init__(self, c: int, m: BitmapNode) -> None: ... | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[K]: ... | ||
|
||
|
||
class MapValues(Generic[V]): | ||
def __init__(self, c: int, m: BitmapNode) -> None: ... | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[V]: ... | ||
|
||
|
||
class MapItems(Generic[K, V]): | ||
def __init__(self, c: int, m: BitmapNode) -> None: ... | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[Tuple[K, V]]: ... | ||
from ._protocols import IterableItems | ||
from ._protocols import MapItems | ||
from ._protocols import MapKeys | ||
from ._protocols import MapMutation | ||
from ._protocols import MapValues | ||
from ._protocols import HT | ||
from ._protocols import KT | ||
from ._protocols import T | ||
from ._protocols import VT_co | ||
|
||
|
||
class Map(Mapping[K, V]): | ||
class Map(Mapping[KT, VT_co]): | ||
@overload | ||
def __init__(self, **kw: V) -> None: ... | ||
def __init__(self) -> None: ... | ||
@overload | ||
def __init__(self: Map[str, VT_co], **kw: VT_co) -> None: ... | ||
@overload | ||
def __init__( | ||
self, __col: Union[IterableItems[KT, VT_co], Iterable[Tuple[KT, VT_co]]] | ||
) -> None: ... | ||
@overload | ||
def __init__( | ||
self, col: Union[Mapping[K, V], Iterable[Tuple[K, V]]], **kw: V | ||
self: Map[Union[KT, str], VT_co], | ||
__col: Union[IterableItems[KT, VT_co], Iterable[Tuple[KT, VT_co]]], | ||
**kw: VT_co | ||
) -> None: ... | ||
def __reduce__(self) -> Tuple[Type[Map], Tuple[dict]]: ... | ||
def __reduce__(self) -> Tuple[Type[Map[KT, VT_co]], Tuple[Dict[KT, VT_co]]]: ... | ||
def __len__(self) -> int: ... | ||
def __eq__(self, other: Any) -> bool: ... | ||
@overload | ||
def update(self, **kw: V) -> Map[str, V]: ... | ||
@overload | ||
def update( | ||
self, col: Union[Mapping[K, V], Iterable[Tuple[K, V]]], **kw: V | ||
) -> Map[K, V]: ... | ||
def mutate(self) -> MapMutation[K, V]: ... | ||
def set(self, key: K, val: V) -> Map[K, V]: ... | ||
def delete(self, key: K) -> Map[K, V]: ... | ||
def get(self, key: K, default: D = ...) -> Union[V, D]: ... | ||
def __getitem__(self, key: K) -> V: ... | ||
def __contains__(self, key: object) -> bool: ... | ||
def __iter__(self) -> Iterator[K]: ... | ||
def keys(self) -> MapKeys[K]: ... | ||
def values(self) -> MapValues[V]: ... | ||
def items(self) -> MapItems[K, V]: ... | ||
def __hash__(self) -> int: ... | ||
def __dump__(self) -> str: ... | ||
def __class_getitem__(cls, item: Any) -> Type[Map]: ... | ||
|
||
|
||
S = TypeVar('S', bound='MapMutation') | ||
|
||
|
||
class MapMutation(MutableMapping[K, V]): | ||
def __init__(self, count: int, root: BitmapNode) -> None: ... | ||
def set(self, key: K, val: V) -> None: ... | ||
def __enter__(self: S) -> S: ... | ||
def __exit__(self, *exc: Any): ... | ||
def __iter__(self) -> NoReturn: ... | ||
def __delitem__(self, key: K) -> None: ... | ||
def __setitem__(self, key: K, val: V) -> None: ... | ||
def pop(self, __key: K, __default: D = ...) -> Union[V, D]: ... | ||
def get(self, key: K, default: D = ...) -> Union[V, D]: ... | ||
def __getitem__(self, key: K) -> V: ... | ||
def __contains__(self, key: Any) -> bool: ... | ||
self, | ||
__col: Union[IterableItems[KT, VT_co], Iterable[Tuple[KT, VT_co]]] | ||
) -> Map[KT, VT_co]: ... | ||
@overload | ||
def update(self, **kw: V) -> None: ... | ||
def update( | ||
self: Map[Union[HT, str], Any], | ||
__col: Union[IterableItems[KT, VT_co], Iterable[Tuple[KT, VT_co]]], | ||
**kw: VT_co # type: ignore[misc] | ||
) -> Map[KT, VT_co]: ... | ||
@overload | ||
def update( | ||
self, col: Union[Mapping[K, V], Iterable[Tuple[K, V]]], **kw: V | ||
) -> None: ... | ||
def finish(self) -> Map[K, V]: ... | ||
def __len__(self) -> int: ... | ||
def __eq__(self, other: Any) -> bool: ... | ||
self: Map[Union[HT, str], Any], | ||
**kw: VT_co # type: ignore[misc] | ||
) -> Map[KT, VT_co]: ... | ||
def mutate(self) -> MapMutation[KT, VT_co]: ... | ||
def set(self, key: KT, val: VT_co) -> Map[KT, VT_co]: ... # type: ignore[misc] | ||
def delete(self, key: KT) -> Map[KT, VT_co]: ... | ||
@overload | ||
def get(self, key: KT) -> Optional[VT_co]: ... | ||
@overload | ||
def get(self, key: KT, default: Union[VT_co, T]) -> Union[VT_co, T]: ... | ||
def __getitem__(self, key: KT) -> VT_co: ... | ||
def __contains__(self, key: Any) -> bool: ... | ||
def __iter__(self) -> Iterator[KT]: ... | ||
def keys(self) -> MapKeys[KT]: ... # type: ignore[override] | ||
def values(self) -> MapValues[VT_co]: ... # type: ignore[override] | ||
def items(self) -> MapItems[KT, VT_co]: ... # type: ignore[override] | ||
def __hash__(self) -> int: ... | ||
def __dump__(self) -> str: ... | ||
def __class_getitem__(cls, item: Any) -> Type[Map[Any, Any]]: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import sys | ||
from typing import Any | ||
from typing import Hashable | ||
from typing import Iterable | ||
from typing import Iterator | ||
from typing import NoReturn | ||
from typing import Optional | ||
from typing import Tuple | ||
from typing import TypeVar | ||
from typing import Union | ||
from typing import overload | ||
|
||
if sys.version_info >= (3, 8): | ||
from typing import Protocol | ||
from typing import TYPE_CHECKING | ||
else: | ||
from typing_extensions import Protocol | ||
from typing_extensions import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from ._map import Map | ||
|
||
HT = TypeVar('HT', bound=Hashable) | ||
KT = TypeVar('KT', bound=Hashable) | ||
KT_co = TypeVar('KT_co', covariant=True) | ||
MM = TypeVar('MM', bound='MapMutation[Any, Any]') | ||
T = TypeVar('T') | ||
VT = TypeVar('VT') | ||
VT_co = TypeVar('VT_co', covariant=True) | ||
|
||
|
||
class MapKeys(Protocol[KT_co]): | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[KT_co]: ... | ||
|
||
|
||
class MapValues(Protocol[VT_co]): | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[VT_co]: ... | ||
|
||
|
||
class MapItems(Protocol[KT_co, VT_co]): | ||
def __len__(self) -> int: ... | ||
def __iter__(self) -> Iterator[Tuple[KT_co, VT_co]]: ... | ||
|
||
|
||
class IterableItems(Protocol[KT_co, VT_co]): | ||
def items(self) -> Iterable[Tuple[KT_co, VT_co]]: ... | ||
|
||
|
||
class MapMutation(Protocol[KT, VT]): | ||
def set(self, key: KT, val: VT) -> None: ... | ||
def __enter__(self: MM) -> MM: ... | ||
def __exit__(self, *exc: Any) -> bool: ... | ||
def __iter__(self) -> NoReturn: ... | ||
def __delitem__(self, key: KT) -> None: ... | ||
def __setitem__(self, key: KT, val: VT) -> None: ... | ||
@overload | ||
def pop(self, __key: KT) -> VT: ... | ||
@overload | ||
def pop(self, __key: KT, __default: T) -> Union[VT, T]: ... | ||
@overload | ||
def get(self, key: KT) -> Optional[VT]: ... | ||
@overload | ||
def get(self, key: KT, default: Union[VT, T]) -> Union[VT, T]: ... | ||
def __getitem__(self, key: KT) -> VT: ... | ||
def __contains__(self, key: object) -> bool: ... | ||
|
||
@overload | ||
def update( | ||
self, | ||
__col: Union[IterableItems[KT, VT], Iterable[Tuple[KT, VT]]] | ||
) -> None: ... | ||
|
||
@overload | ||
def update( | ||
self: 'MapMutation[Union[HT, str], Any]', | ||
__col: Union[IterableItems[KT, VT], Iterable[Tuple[KT, VT]]], | ||
**kw: VT | ||
) -> None: ... | ||
@overload | ||
def update(self: 'MapMutation[Union[HT, str], Any]', **kw: VT) -> None: ... | ||
def finish(self) -> 'Map[KT, VT]': ... | ||
def __len__(self) -> int: ... | ||
def __eq__(self, other: Any) -> bool: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[mypy] | ||
incremental = True | ||
strict = True | ||
|
||
[mypy-immutables.map] | ||
ignore_errors = True | ||
|
||
[mypy-immutables._testutils] | ||
ignore_errors = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# We need the mypy pytest plugin to do the test collection for our | ||
# typing tests. | ||
|
||
# mypy demands that its test-data be present for mypy.test.config to be | ||
# imported, so thwart that check. mypy PR #10919 fixes this. | ||
import unittest.mock | ||
with unittest.mock.patch('os.path.isdir') as isdir: | ||
isdir.return_value = True | ||
import mypy.test.config # noqa | ||
|
||
pytest_plugins = [ | ||
'mypy.test.data', | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for adding
mypy.ini
in the distribution?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I did this out of habit. Most of the projects I work on do some sort of type checking during testing. That doesn't seem to be the case for this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think it is good idea to add such a test, otherwise the annotations will inevitably rot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elprans would you like me to add a test that runs mypy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that would work too.