diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml new file mode 100644 index 0000000..df1875e --- /dev/null +++ b/.github/workflows/pypi.yml @@ -0,0 +1,29 @@ +name: PyPi + +on: + push: + tags: + - "v*.*.*" + +jobs: + publish_pypi: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Install + run: pip3 install --quiet --upgrade setuptools wheel twine + - name: Build + run: python3 setup.py sdist bdist_wheel + - name: Publish + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: ${{ secrets.TEST_PYPI_TOKEN }} + TWINE_REPOSITORY_URL: https://test.pypi.org/legacy/ + run: twine upload dist/* + diff --git a/Makefile b/Makefile index e2da7d8..86a55d1 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ +.PHONY: install +install: + pip3 install --quiet --upgrade pip + pip3 install --quiet -r requirements.txt -r requirements-dev.txt + .PHONY: lint lint: python3 -m flake8 && python3 -m isort -m VERTICAL_HANGING_INDENT --check-only . diff --git a/README.md b/README.md index a27f446..6f5b619 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,132 @@ # d42-typing -Local install: +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](#features) +- [How it works](#how-it-works) +- [Example](#example) + - [Scalar schema](#scalar-schema) + - [Dict schema](#dict-schema) + - [Working type hints for PyCharm](#working-type-hints-for-pycharm) +- [Installation & Usage](#installation--usage) + - [How to configurate type auto-generation in PyCharm](#how-to-configurate-type-auto-generation-in-pycharm) + + +## 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 +```python +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 +```python +# --- 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 + ``` -pip install -e . + +#### Working type hints for PyCharm + +drawing + +## Installation & Usage + +To install `d42-typing`, use the following command: + +```sh +pip install d42-typing ``` -Run +To generate type hints, run the following command: + +```sh +d42-typing --path-to-schemas scenarios/schemas -a -v +# d42-typing --help ``` -d42-typing --help -d42-typing --path-to-schemas _tests/schemas/new -``` \ No newline at end of file + +### 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 \ No newline at end of file diff --git a/assets/file_watcher_1.png b/assets/file_watcher_1.png new file mode 100644 index 0000000..6682f5f Binary files /dev/null and b/assets/file_watcher_1.png differ diff --git a/assets/file_watcher_2.png b/assets/file_watcher_2.png new file mode 100644 index 0000000..7516edc Binary files /dev/null and b/assets/file_watcher_2.png differ diff --git a/assets/type_hints.png b/assets/type_hints.png new file mode 100644 index 0000000..dd66173 Binary files /dev/null and b/assets/type_hints.png differ diff --git a/setup.py b/setup.py index 3068fe7..08cbafc 100644 --- a/setup.py +++ b/setup.py @@ -17,12 +17,22 @@ def find_dev_required(): name="d42-typing", version="0.0.2", description=".pyi typing stubs generation for d42 schemas", + url="https://github.com/mytestopia/d42-typing", + long_description=open("README.md").read(), + long_description_content_type="text/markdown", install_requires=find_required(), + tests_require=find_dev_required(), entry_points={ 'console_scripts': ['d42-typing=app.main:main'] }, author="Anna", author_email="testopia13@gmail.com", - packages=find_packages(), + packages=find_packages(exclude=["tests"]), python_requires='>=3.10', + license="Apache-2.0", + classifiers=[ + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + ], )