Skip to main content

.pyi typing stubs generation for d42 schemas

Project description

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.

Example

Scalar schema

from d42 import schema

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

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

ValueSchema: Union[IntSchema, FloatSchema]

# --- blahblah.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]

# --- blahblah.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

To generate type hints, run the following command:

d42-typing --path-to-schemas scenarios/schemas -a -v
# d42-typing --help

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-0.0.2.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

d42_typing-0.0.2-py3-none-any.whl (49.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: d42_typing-0.0.2.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for d42_typing-0.0.2.tar.gz
Algorithm Hash digest
SHA256 dd158a70592787ff74320ce4b28d4a5e66fc15217887fc3d83a80524be828dcb
MD5 401bab673a14acefa8b7bea698cb0e73
BLAKE2b-256 0e6ae01bc52ea4d7346a35551d151707f11f3273e6ed2842226ac34d5facc175

See more details on using hashes here.

File details

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

File metadata

  • Download URL: d42_typing-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for d42_typing-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eccd681db7b1b594dc4f152a3537fca4943c77212a565c10d996c027e4740f29
MD5 4b945a88362eda37ff6c9ea3ca2de357
BLAKE2b-256 eb9d107b8bf737f861a7e0c986f84b155806f9bce6bf2ec9962dee8ba60b9019

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