Skip to main content

A collection of extra types and features for mypy

Project description

mypy-extras

wemake.services test codecov Python Version wemake-python-styleguide

Features

  • Provides a custom mypy plugin to enhance its possibilities
  • Provides new types that can be used in your programs with our plugin
  • Fully typed with annotations and checked with mypy, PEP561 compatible

Installation

pip install mypy-extras

You also need to configure mypy correctly and install our custom plugin:

# In setup.cfg or mypy.ini:
[mypy]
plugins =
  mypy_extras.plugin.entrypoint

We also recommend to use the same mypy settings we use.

Usage

AttrOf

We provide a special type to get named attributes of other types, like so:

from typing_extensions import Literal  # or typing on python3.8+
from mypy_extras import AttrOf

class User(object):
    def auth(self, username: str, password: str) -> bool:
        return False  # Just an example

def get_callback(user: User) -> AttrOf[User, Literal['auth']]:
    return user.auth

user: User
reveal_type(get_callback(user))
# Revealed type is 'def (username: builtins.str, password: builtins.str) -> builtins.bool'

ensure_attr

We can ensure that some str attribute exists on a object:

from mypy_extras import ensure_attr


class User(object):
    policy = 'update'


reveal_type(ensure_attr(User, 'policy'))  # Revealed type is 'Literal['policy']'
reveal_type(ensure_attr(User, 'missing'))  # Error: attribute "missing" does not exist on type "User"

It is useful when we do any manipulations with objects based on a string field:

DEFAULT_POLICY_FIELD: Final = ensure_attr(User, 'policy')  # typesafe
# vs
DEFAULT_POLICY_FIELD: Final = 'policy'  
# User can rename the field, and this will blow now!

License

MIT

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

mypy-extras-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

mypy_extras-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file mypy-extras-0.1.0.tar.gz.

File metadata

  • Download URL: mypy-extras-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Darwin/18.7.0

File hashes

Hashes for mypy-extras-0.1.0.tar.gz
Algorithm Hash digest
SHA256 75fc61e95c53ac74d17ce4a746dc37fe7b50acd2c942f1ef462ef17dde65ae94
MD5 2945eeb7c3c5bdbcfaef3bb87f793628
BLAKE2b-256 184b93727b8f4cfbfda6d300df6963a8722ad14c4fb6ffd127a7fd75f610d97b

See more details on using hashes here.

Provenance

File details

Details for the file mypy_extras-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mypy_extras-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Darwin/18.7.0

File hashes

Hashes for mypy_extras-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1387e1b696540762512b725cb858f838aab2dc07da7016291fbeaa840634596
MD5 4b90b32088e314181a7119721e102ee1
BLAKE2b-256 ac624090038ca61a64dc4e3bc7dd5161b9539c43f15d77d315ed2259e827a40b

See more details on using hashes here.

Provenance

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