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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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