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

Uploaded Source

Built Distribution

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

uglyduck-0.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: uglyduck-0.0.1.tar.gz
  • Upload date:
  • Size: 8.1 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.1.tar.gz
Algorithm Hash digest
SHA256 45cf49e02b1f5a58d95098a7855dfd7bfd5a1d76b6ff1882bc0147367d520c48
MD5 40c3f35dcbfd3fe44c0c5e2a6342bb19
BLAKE2b-256 afe4be80d099fd146ad5cc8bd01eba0387c91835c68be4ca73fd1ab70e625298

See more details on using hashes here.

File details

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

File metadata

  • Download URL: uglyduck-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9d55b8ea1980b42f9d4c6e1c8912662b6f0ca88be438d4f11ad88f4e1e9b869
MD5 2debbfa2082a36dfb5aa70da65c8a842
BLAKE2b-256 d4fa229d8da38707651f3d7640730b3124d6576a07f4a581f322e8f4ab08dbc2

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