Skip to main content

Generate a types.py file for a package that contains protocols for classes generated from the files specified.

Project description

Ugly Duck

Use this module to make protocols based on classes in your python project. This project was made out of frustration with the current state of static types in Python 3, mainly juggling with circular imports and the workaround of importing with TYPE_CHECKING. I originally made this to generate a starting point for one of my projects, but I ended up enjoying working with it and ended up setting it up to work in real-time in other projects. It makes working with static typing in Python as easy as it is in other languages. All protocols are the class name with an 'I' prefix. For example, a class named 'User' will have a protocol named 'IUser'. You can then import IUser from the types.py file and use it anywhere and not have to think about/manage imports. It's ugly, I know.

Installation

pip install uglyduck

Usage

Include the following in a package's __init__.py file:

from uglyduck.inspector import TypeInspector

# This will create packagename/types.py with protocols for all classes in the
# models and helpers modules.
TypeInspector.make_package_types_file('packagename', modules=[
    'models',
    'helpers'
])

If you're using Django, you will need to run it after the app is ready in your apps.py file:

from django.conf import settings
from django.apps import AppConfig

from uglyduck.inspector import TypeInspector


class MyAppConfig(AppConfig):
    name = "apps.myapp"

    def ready(self):
        if settings.DEBUG:
            TypeInspector.make_package_types_file('apps.myapp', modules=[
                'models',
                'helpers'
            ])

Possible Improvements

  • Support inheritance from classes outside of the package.

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

uglyduck-0.0.3.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

uglyduck-0.0.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file uglyduck-0.0.3.tar.gz.

File metadata

  • Download URL: uglyduck-0.0.3.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for uglyduck-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ce06526ed4f0d4ff4b48fb61b56d1a7630c091f08cbfd728f9b3028445b047d8
MD5 de4033bd11fb2f3b8af627158a3215bb
BLAKE2b-256 f0253ab3b7c71a3ac08c4c7027394f8f53ce48d361b6ee01665c86e8dc23b66b

See more details on using hashes here.

File details

Details for the file uglyduck-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: uglyduck-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for uglyduck-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 40277b968704e586b07174aebd1d2b11776f6e373a733958b08747317d3fd1d6
MD5 0d2d31c34ed411975aab66d5c9ea8f9d
BLAKE2b-256 6cf0e013da478cea702ecf80494c55ddc53b340c371fd2f2c6cc479714ec61ee

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