Skip to main content

.pyi typing stubs generation for d42 schemas

Project description

PyPI Python Version

d42-typing

d42-typing is a Python package designed to enhance type-checking capabilities within the d42 ecosystem. This package introduces generated type definitions that make it easier to work with D42 schemas by providing a more structured and robust type-checking mechanism.

Features

  • Type Definitions: Provides comprehensive type definitions for various entities within Device42.
  • Improved Type Checking: Enhances code safety and reliability by utilizing Python's type hints.

How it works

  • Generates Python type hints from d42 schemas.
  • Creates .pyi files for each schema file in a specified folder (or default).
  • Provides overloads for the fake method from d42 library in stubs folder.

Example

Scalar schema

from d42 import schema

# --- scalar.py
ValueSchema = schema.int | schema.float

# --- scalar.pyi 
from ... import ...

ValueSchema: Union[IntSchema, FloatSchema]

# --- _stubs/d42/fake.pyi
from ... import ...

@overload
def fake(schema: ValueSchema) -> Union[int, float]:
    pass

Dict schema

# --- dict.py
from d42 import schema

DictSchema = schema.dict({
    'id': schema.int,
    'name': schema.str('default_name') | schema.str('custom_name'),
    'phone': schema.str | schema.none,
})

# --- dict.pyi
from ... import ...

class _D42MetaUserSchema(type):

    @overload
    def __getitem__(cls, arg: Literal['id']) -> IntSchema:
        pass

    @overload
    def __getitem__(cls, arg: Literal['name']) -> StrSchema:
        pass

    @overload
    def __getitem__(cls, arg: Literal['phone']) -> Union[StrSchema, NoneSchema]:
        pass

    def __mod__(self, other):
        pass

    def __add__(self, other):
        pass

class UserSchema(metaclass=_D42MetaUserSchema):

    class type(TypedDict, total=False):
        id: IntSchema.type
        name: StrSchema.type
        phone: Union[StrSchema.type, NoneSchema.type]

# --- _stubs/d42/fake.pyi
from typing import overload
from typing import Type
from _tests.schemas.test import UserSchema

@overload
def fake(schema: Type[UserSchema]) -> UserSchema.type:
    pass

Working type hints for PyCharm

drawing

Installation & Usage

To install d42-typing, use the following command:

pip install d42-typing
  1. Generate type hints, run the following command:
d42-typing --path-to-schemas scenarios/schemas -a -v -s _stubs
# d42-typing --help
  1. Configure mypy for correct type-checking:
[mypy]
mypy_path = _stubs
  1. Add _stubs/ directory in .gitignore.
  2. Mark _stubs/ directory as Sources Root in PyCharm.

How to configurate type auto-generation in PyCharm

  1. Set FileWatcher in PyCharm for auto-generating stubs

    • Go to Pycharm → Settings → Tools → File Watchers
    • Set the scope pattern: file[project]:packages/e2e/schemas/*.py

    drawing drawing

  2. Hide .pyi files (if needed):

    Go to Settings → Editor → File Types → Ignored Files and Folders tab

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

d42_typing-1.0.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

d42_typing-1.0.0-py3-none-any.whl (52.9 kB view details)

Uploaded Python 3

File details

Details for the file d42_typing-1.0.0.tar.gz.

File metadata

  • Download URL: d42_typing-1.0.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for d42_typing-1.0.0.tar.gz
Algorithm Hash digest
SHA256 68b89a70524c86d3eb14e960f0894c45c186992031c3302c242d60828ba2a291
MD5 c541375c2da87db176a4b0c78123c75f
BLAKE2b-256 731f79e5a923e94e07a0083b13ccacdd93478e6d9a85c839e91a8e89450fb640

See more details on using hashes here.

File details

Details for the file d42_typing-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: d42_typing-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 52.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for d42_typing-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d2f05e9ad1fcf9781b6f6ca23054e4ac684518b93fc89687d00e751edb545df
MD5 fe17d2643963a4a405f46d34f08827d9
BLAKE2b-256 9e8e3ef55ae1701d2722725781bfdb8535d38a32212b5b92c400d7096a2de025

See more details on using hashes here.

Supported by

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