Skip to main content

Static + runtime class declaration level structural types/interface validation

Project description

Struct Types

Static + runtime class declaration level structural types/interface validation

In other words, the implements keyword from programming languages such as Typescript. Take better advantage of your protocols and be even more sure you correctly implement their interfaces, even in brownfield projects.

Examples

Explicit Syntax API

Recommended. Explicitly indicates code context without resorting to internal documentation or possibly unknown conventions.

from struct_types import interfaces, implements

class EmailService:
    def __init__(self):
        interfaces(
            implements(Notifier).on(self),
            implements(Sender).on(self),
            implements(Object).on(self),
        )

Succinct Syntax API

Terser alternative API.

from struct_types import of

class EmailService:
    def __init__(self):
        of(Notifier).on(self)
        of(Sender).on(self)
        of(Object).on(self)

Runtime API

Guard at runtime if a type correctly implements a protocol, raising an error if the types are mismatched. Note type hints are still defined for static type analysis and 3rd party library runtime type analysis.

from struct_types import does

class EmailService:
    def __init__(self):
        does(Notifier).compile(EmailService)
        does(Sender).compile(EmailService)
        does(Object).compile(EmailService)

Union API

Indicates the type must implement at least one of the protocols much like a union type.

from struct_types import union

class EmailService:
    def __init__(self):
        union(Notifier, Sender, Object).on(EmailService)

Note: The .on method can interchangeably use either a reference to the type itself or an instance of a type. For consistency, prefer the co-locating within the constructor and using the reference to self.

See Also

  • Protocols. These are Python's equivalent of interfaces as found in other familiar programming languages such as Typescript and Java, and are the data structures that Struct Types has been designed for.
  • Beartype or Pydantic. Struct Types is not a replacement for runtime type and/or data validation.

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

struct_types-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

struct_types-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file struct_types-0.1.0.tar.gz.

File metadata

  • Download URL: struct_types-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.12.0

File hashes

Hashes for struct_types-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7cb1945a216fe67efea39fc6b62af8dddab75135f1adf47e440d74168f77b649
MD5 c59745443e40ab89027209353617308f
BLAKE2b-256 430a7b3afbd20046381404cf779f41b8001910a1173266be2a10d4ad16794a9f

See more details on using hashes here.

File details

Details for the file struct_types-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: struct_types-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.12.0

File hashes

Hashes for struct_types-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70faaede30024f5747bc778cd4581858bfe7064f1408054ba427f2e89f5723c9
MD5 eb2ec7624c5e728b666c2ffd2a5b80ec
BLAKE2b-256 41e84fb159e996e7aedd53bfdbb341d1d3d8ba2d7bc54b95e9093a249325782f

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