Skip to main content

Helper for checking variable equivalence by type. Useful for tests.

Project description

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/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

type_comparable-0.3.dev9.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

type_comparable-0.3.dev9-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file type_comparable-0.3.dev9.tar.gz.

File metadata

  • Download URL: type_comparable-0.3.dev9.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for type_comparable-0.3.dev9.tar.gz
Algorithm Hash digest
SHA256 e71d6ca8c494a3f1fc2bfa64b8ed96054d0c54bcdde9a88ca6077ec2c5212667
MD5 16bba73e0e77ceb8d64ea5fc598cf46c
BLAKE2b-256 3b0f78d2effe35572af1e32f1a71ce87638fc86204954e454254e5df93c592e3

See more details on using hashes here.

File details

Details for the file type_comparable-0.3.dev9-py3-none-any.whl.

File metadata

  • Download URL: type_comparable-0.3.dev9-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for type_comparable-0.3.dev9-py3-none-any.whl
Algorithm Hash digest
SHA256 08a6c190b16da639f6191a9e6e382037dd000e6457ccabadd39ab1caee349abc
MD5 498717a27df33e957ce852497a1cb091
BLAKE2b-256 3b46eef5184a704df5e79e2eaa1685c6ac3f0f3e7133ec7f486c93b50b384e3b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page