Skip to main content

Python module to enforce strong typing

Project description

strongly_typed

This is a python decorator that allows you to type-check functions. Itll check the type signature of the function and, if the signature and the parameters given mismatch, will raise an exception.

Inspiration

I wanted to use this to make sure that software i was going to write was resilient for data integrity reasons. I've also learned a lot about decorators, etc.

Usage

from strongly_typed import strongly_typed
@strongly_typed
def add(a: int, b: int, message_template: str):
    result = a + b
    message = message_template % result
    return message

add(1, 2, "The answer is %s!")
add(1, "e", 5) # raises an exception!

Known issues

  • *args and **kwargs are not tested at all. This is because I'm not sure how I'd implement that. (Plus, I don't think it's very common to annotate those.)

  • Using "interchangeable" types (e.g. using an int when a float is required, or vice versa) will raise an exception. This is by design. The whole idea is to prevent python's weak typing from screwing things up. Type coercion isn't the only way it can do that, but it's one of them - and not always caught by linters.

  • Subclasses are not checked. So:

class Hello:
    def hi(self):
        print("hi")

class Subclass(Hello):
    def __init__(self):
        print("Init!")

@strongly_typed.strongly_typed
def run_hi(cls: Hello):
    cls.hi()

run_hi(Hello()) # OK
run_hi(Subclass()) # Raises TypeError

There may be an optional feature to check if it's a subclass, rather than the absolute type. That's not a thing yet, though.

Contributing

The code is probably terrible. Please help me fix it! If you have any suggestions, please let me know.

Licence

Copyright 2023 TheTechRobo Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

strongly_typed-1.0.1.post3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

strongly_typed-1.0.1.post3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file strongly_typed-1.0.1.post3.tar.gz.

File metadata

  • Download URL: strongly_typed-1.0.1.post3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.9.7

File hashes

Hashes for strongly_typed-1.0.1.post3.tar.gz
Algorithm Hash digest
SHA256 95394c3ff3731faa1583707c3ec82ea20df263c536ded42ce2da13eb3716d79f
MD5 45478c29060de0d3c033d69597dc4970
BLAKE2b-256 e1bbd787a508b848944afdf5353706e22c5c0be0d06d412b6f39d55a6c453569

See more details on using hashes here.

File details

Details for the file strongly_typed-1.0.1.post3-py3-none-any.whl.

File metadata

  • Download URL: strongly_typed-1.0.1.post3-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.9.7

File hashes

Hashes for strongly_typed-1.0.1.post3-py3-none-any.whl
Algorithm Hash digest
SHA256 e7f5c018f3036249b2db17dc0e21ed7b997d5f490aeca75c9527451c8bfffc24
MD5 e7c392a0181380aa8d721d065c0a7701
BLAKE2b-256 d23f991812b11326caca88c6586f5a888c59efaa8e4494df50279f435ab86c51

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