Skip to content

Commit

Permalink
refactor: rename package into typingx
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Jan 17, 2021
1 parent d04b8e7 commit f42a1de
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 212 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.DEFAULT_GOAL := all
black = black typing_extend tests
isort = isort typing_extend tests
black = black typingx tests
isort = isort typingx tests

.PHONY: install
install:
Expand All @@ -23,10 +23,10 @@ format:

.PHONY: lint
lint:
poetry run flake8 typing_extend tests
poetry run flake8 typingx tests
poetry run ${black} --diff --check
poetry run ${isort} --check-only
poetry run mypy typing_extend
poetry run mypy typingx

.PHONY: all
all: lint test
123 changes: 62 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# typing-extend
[![Tests](https://github.com/PrettyWood/typing-extend/workflows/Tests/badge.svg)](https://github.com/PrettyWood/typing-extend/actions)
[![codecov](https://codecov.io/gh/PrettyWood/typing-extend/branch/main/graph/badge.svg)](https://codecov.io/gh/PrettyWood/typing-extend)
[![pypi](https://img.shields.io/pypi/v/typing-extend.svg)](https://pypi.python.org/pypi/typing-extend)
[![versions](https://img.shields.io/pypi/pyversions/typing-extend.svg)](https://github.com/PrettyWood/typing-extend)
[![license](https://img.shields.io/github/license/PrettyWood/typing-extend.svg)](https://github.com/PrettyWood/typing-extend/blob/master/LICENSE)
# typingx
[![Tests](https://github.com/PrettyWood/typingx/workflows/Tests/badge.svg)](https://github.com/PrettyWood/typingx/actions)
[![codecov](https://codecov.io/gh/PrettyWood/typingx/branch/main/graph/badge.svg)](https://codecov.io/gh/PrettyWood/typingx)
[![pypi](https://img.shields.io/pypi/v/typingx.svg)](https://pypi.python.org/pypi/typingx)
[![versions](https://img.shields.io/pypi/pyversions/typingx.svg)](https://github.com/PrettyWood/typingx)
[![license](https://img.shields.io/github/license/PrettyWood/typingx.svg)](https://github.com/PrettyWood/typingx/blob/master/LICENSE)

`typing` is great but it changed a lot since 3.6 and it's not over!

Expand All @@ -13,89 +13,90 @@ and go even further with `typing` (and `typing_extensions`).
It provides:
- `get_args` and `get_origin` for python `3.6` to `3.9` that mimic `3.10` behaviour
- `is_literal`, `is_typeddict` helpers
- most `typing` types but with homogeneous behaviour (e.g. with `3.8`, `typing.TypedDict` won't store information to distinguish optional and required keys)
- most `typing` types but with homogeneous behaviour
(e.g. with `3.8`, `typing.TypedDict` won't store information to distinguish optional and required keys. This lib will hence choose `typing_extensions` version)

but also:
- `xisinstance`: like `isinstance` but with `typing` types
- `isinstancex`: like `isinstance` but with `typing(x)` types
- extra types:
* `XList` and `XTuple`: more sophisticated versions of `List` and `Tuple` to add `...` anywhere in the parameters
* `Listx` and `Tuplex`: more sophisticated versions of `List` and `Tuple` to add `...` anywhere in the parameters
- extanded types:
* `TypedDict` has a `__extra__` field (value can be changed) to allow type checking on optional fields

## Installation

``` bash
pip install typing_extend
pip install typingx
```

## Usage
```python
from typing_extend import (
from typingx import (
Any,
Dict,
List,
Listx,
Literal,
Set,
Tuple,
Tuplex,
Type,
TypedDict,
Union,
XList,
XTuple,
xisinstance,
isinstancex,
)

# Dict
assert xisinstance({"a": 1, "b": 2}, Dict[str, int]) is True
assert xisinstance({"a": 1, "b": 2}, Dict[str, str]) is False
assert xisinstance({"a": 1, "b": 2}, Dict[int, str]) is False
assert xisinstance({"a": 1, "b": 2}, Dict[str, Any]) is True
assert isinstancex({"a": 1, "b": 2}, Dict[str, int]) is True
assert isinstancex({"a": 1, "b": 2}, Dict[str, str]) is False
assert isinstancex({"a": 1, "b": 2}, Dict[int, str]) is False
assert isinstancex({"a": 1, "b": 2}, Dict[str, Any]) is True

# List
assert xisinstance([1, 2, 3], List[int]) is True
assert xisinstance([1, 2, "q"], List[int]) is False
assert xisinstance([1, 2, "q"], List[Union[str, int]]) is True

# XList
assert xisinstance([1, 2, 3, 4], XList[int]) is True
assert xisinstance([1, 2, "q"], XList[int, ..., str]) is True
assert xisinstance([1, 2, "q", "w", "e"], XList[int, ..., str]) is False
assert xisinstance([1, 2, "q", "w", "e"], XList[int, ..., str, ...]) is True
assert xisinstance([1, 2, "q", "w", b"xyz", "e"], XList[int, ..., str, ...]) is False
assert xisinstance([1, 2, "q", "w", b"xyz", "e"], XList[int, ..., Union[str, bytes], ...]) is True
assert isinstancex([1, 2, 3], List[int]) is True
assert isinstancex([1, 2, "q"], List[int]) is False
assert isinstancex([1, 2, "q"], List[Union[str, int]]) is True

# Listx
assert isinstancex([1, 2, 3, 4], Listx[int]) is True
assert isinstancex([1, 2, "q"], Listx[int, ..., str]) is True
assert isinstancex([1, 2, "q", "w", "e"], Listx[int, ..., str]) is False
assert isinstancex([1, 2, "q", "w", "e"], Listx[int, ..., str, ...]) is True
assert isinstancex([1, 2, "q", "w", b"xyz", "e"], Listx[int, ..., str, ...]) is False
assert isinstancex([1, 2, "q", "w", b"xyz", "e"], Listx[int, ..., Union[str, bytes], ...]) is True

# Literal
assert xisinstance("a", Literal["a"]) is True
assert xisinstance(Literal["a"], Literal["a"]) is True
assert xisinstance("b", Literal["a"]) is False
assert xisinstance("b", Literal["a", Literal[Literal["b"]]]) is True
assert xisinstance(Literal["a", "b"], Literal["b", "a", "c"]) is True
assert isinstancex("a", Literal["a"]) is True
assert isinstancex(Literal["a"], Literal["a"]) is True
assert isinstancex("b", Literal["a"]) is False
assert isinstancex("b", Literal["a", Literal[Literal["b"]]]) is True
assert isinstancex(Literal["a", "b"], Literal["b", "a", "c"]) is True

# Set
assert xisinstance({"a", "b"}, Set[str]) is True
assert xisinstance({"a", "b"}, Set[int]) is False
assert isinstancex({"a", "b"}, Set[str]) is True
assert isinstancex({"a", "b"}, Set[int]) is False

# Tuple
assert xisinstance((1, 2), Tuple[int, ...]) is True
assert xisinstance((1, 2), Tuple[int, int]) is True
assert xisinstance((1, 2), Tuple[int, int, int]) is False

# XTuple
assert xisinstance((3, "a", "b"), XTuple[int, str, ...]) is True
assert xisinstance((3, "a", "b", "c"), XTuple[int, str, ...]) is True
assert xisinstance((3, "a", "b", "c"), XTuple[int, str, ..., bool]) is False
assert xisinstance((3, "a", "b", "c", True), XTuple[int, str, ..., bool]) is True
assert xisinstance((3, "a", "b", "c", 3), XTuple[int, str, ..., bool]) is False
assert xisinstance((3, "a", "b", "c", True, False), XTuple[int, str, ..., bool, ...]) is True
assert isinstancex((1, 2), Tuple[int, ...]) is True
assert isinstancex((1, 2), Tuple[int, int]) is True
assert isinstancex((1, 2), Tuple[int, int, int]) is False

# Tuplex
assert isinstancex((3, "a", "b"), Tuplex[int, str, ...]) is True
assert isinstancex((3, "a", "b", "c"), Tuplex[int, str, ...]) is True
assert isinstancex((3, "a", "b", "c"), Tuplex[int, str, ..., bool]) is False
assert isinstancex((3, "a", "b", "c", True), Tuplex[int, str, ..., bool]) is True
assert isinstancex((3, "a", "b", "c", 3), Tuplex[int, str, ..., bool]) is False
assert isinstancex((3, "a", "b", "c", True, False), Tuplex[int, str, ..., bool, ...]) is True

# Type
class User: ...
class BaseUser(User): ...

assert xisinstance(BaseUser, Type[BaseUser]) is True
assert xisinstance(BaseUser, Type[User]) is True
assert xisinstance(User, Type[User]) is True
assert xisinstance(User, Type[BaseUser]) is False
assert isinstancex(BaseUser, Type[BaseUser]) is True
assert isinstancex(BaseUser, Type[User]) is True
assert isinstancex(User, Type[User]) is True
assert isinstancex(User, Type[BaseUser]) is False

# TypedDict
FullMovie = TypedDict("FullMovie", {"name": str, "year": int})
Expand All @@ -109,16 +110,16 @@ class ExtraMovie(TypedDict):
year: int
__extra__: str

assert xisinstance({"name": "The Matrix", "year": 1999}, FullMovie) is True
assert xisinstance({"name": "The Matrix", "year": "1999"}, FullMovie) is False
assert xisinstance({"name": "The Matrix"}, FullMovie) is False
assert xisinstance({"name": "The Matrix", "year": 1999, "extra": "qwe"}, FullMovie) is False
assert isinstancex({"name": "The Matrix", "year": 1999}, FullMovie) is True
assert isinstancex({"name": "The Matrix", "year": "1999"}, FullMovie) is False
assert isinstancex({"name": "The Matrix"}, FullMovie) is False
assert isinstancex({"name": "The Matrix", "year": 1999, "extra": "qwe"}, FullMovie) is False

assert xisinstance({"name": "The Matrix", "year": 1999}, PartialMovie) is True
assert xisinstance({"name": "The Matrix"}, PartialMovie) is True
assert xisinstance({"name": "The Matrix", "year": 1999, "extra": "qwe"}, PartialMovie) is False
assert isinstancex({"name": "The Matrix", "year": 1999}, PartialMovie) is True
assert isinstancex({"name": "The Matrix"}, PartialMovie) is True
assert isinstancex({"name": "The Matrix", "year": 1999, "extra": "qwe"}, PartialMovie) is False

assert xisinstance({"name": "The Matrix", "year": 1999}, ExtraMovie) is True
assert xisinstance({"name": "The Matrix", "year": 1999, "q": "w", "e": "r"}, ExtraMovie) is True
assert xisinstance({"name": "The Matrix", "year": 1999, "q": "w", "e": 1}, ExtraMovie) is False
assert isinstancex({"name": "The Matrix", "year": 1999}, ExtraMovie) is True
assert isinstancex({"name": "The Matrix", "year": 1999, "q": "w", "e": "r"}, ExtraMovie) is True
assert isinstancex({"name": "The Matrix", "year": 1999, "q": "w", "e": 1}, ExtraMovie) is False
```
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[tool.poetry]
name = "typing-extend"
name = "typingx"
version = "0.1.0"
description = "Extend typing package functionalities"
authors = ["Eric Jolibois <[email protected]>"]
license = "MIT"
repository = "https://github.com/PrettyWood/typing-extend"
homepage = "https://github.com/PrettyWood/typing-extend"
repository = "https://github.com/PrettyWood/typingx"
homepage = "https://github.com/PrettyWood/typingx"
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -48,7 +48,7 @@ force_grid_wrap = 0
use_parentheses = true

[tool.pytest.ini_options]
addopts = "--cov typing_extend --cov-report=term-missing --cov-report=xml"
addopts = "--cov typingx --cov-report=term-missing --cov-report=xml"
testpaths = ["tests"]

[build-system]
Expand Down
Loading

0 comments on commit f42a1de

Please sign in to comment.