About
Module allows to compare variables not only by value but by type too.
Quick example:
from typing import Any
from type_comparable import make_type_comparable
response = {
'id': 144233,
'date_create': '2020-01-25T17:31:33.910803',
'important_data': 'important data',
'other_data': 'other data',
'inner_data': {
'field a': 'value a',
'field d': 'value b'
},
'line': [1, 'some text', 3]
}
assert make_type_comparable(response) == {
'id': int, # <-- will compare by type int
'date_create': str, # < -- will compare by type str
'important_data': 'important data', # <-- exact match as is
'other_data': Any, # <-- allow any data,
'inner_date': { # <-- also work with nested dictionaries
'field a': str,
'field b': 'value b'
}
'line': [int, Any, 3] # <- check elements in array
}
# if you don't want wrap left variable (response) if can wrap right:
assert response == make_type_comparable(...)
Very useful for tests by pytest.
Support types
Comparable types (which can be passed to make_type_comparable()):
int
bool
str
list
dict
other
Types for comparison:
all python builtin (int, str, bool, list, dict, etc.)
object and typing.Any - mean any type but not None
typing.Optional - mean any type and None. Optional[int] now not supported
Also you can try to use with your custom types but without guaranteed (verify manually before use in product)
Know issues
Wrapped None is not None :-(
>> make_type_comparable(None) is None
False
# use equal
>> make_type_comparable(None) == None
True
Install
From PyPi:
$ pip install type_comparable
From local:
# update setuptools
$ pip install 'setuptools >= 30.4'
# do install
$ make install
# or
$ pip install .
Development
Prepare and activate virtual environment like:
$ python3 -m venv .env
# for bash
$ source .env/bin/activate
# for fish
$ . .env/bin/activate.fish
Update pre-install dependencies:
$ pip install 'setuptools >= 30.4'
Install:
$ make install_dev
# or
$ pip install --editable .[develop]
Run tests:
$ make test
# or
$ pytest tests/
Metadata
Release files for type-comparable 2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| type_comparable-2.tar.gz | 7.6 kB | Without provenance | Details for type_comparable-2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| type_comparable-2-py3-none-any.whl | Python 3 | none | any | Details for type_comparable-2-py3-none-any.whl |
Total release size: 12.4 kB
Release files / type_comparable-2.tar.gz
| Download URL | type_comparable-2.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ab8e0627483563f158324514b459be0004fd8c03016005f8d1a9170e8df236a9
|
|
BLAKE2b-256 checksum How to use checksums |
d110b49801874c9f7e5eab9bb48d7c592b0d6b1031d162698feedffa9d748d77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / type_comparable-2-py3-none-any.whl
| Download URL | type_comparable-2-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c66978b2a87883c480f7c848e42018a0d33a809a6dd0827f321f5a6d8a921ed2
|
|
BLAKE2b-256 checksum How to use checksums |
eed147375be7f3ddef02b5a9471557b85c9b104303fceed3d35027536e5eb553
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Security for type-comparable 2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.