Skip to main content

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

Metadata

Release files for mypy-extras 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mypy-extras 0.1.0
File Size Uploaded
mypy-extras-0.1.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mypy-extras 0.1.0
File Interpreter ABI Platform
mypy_extras-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / mypy-extras-0.1.0.tar.gz

Download URL mypy-extras-0.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
75fc61e95c53ac74d17ce4a746dc37fe7b50acd2c942f1ef462ef17dde65ae94
BLAKE2b-256 checksum
How to use checksums
184b93727b8f4cfbfda6d300df6963a8722ad14c4fb6ffd127a7fd75f610d97b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Darwin/18.7.0

Release files / mypy_extras-0.1.0-py3-none-any.whl

Download URL mypy_extras-0.1.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1387e1b696540762512b725cb858f838aab2dc07da7016291fbeaa840634596
BLAKE2b-256 checksum
How to use checksums
ac624090038ca61a64dc4e3bc7dd5161b9539c43f15d77d315ed2259e827a40b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Darwin/18.7.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page