Typethon is an extremely lightweight and simple module for strongly typed Python.
Typethon has zero dependencies, nada, zilch! Typethon doesn't even use imports from the standard library! (except for argument parsing in the CLIs, but they are completely optional and only provide linting / building functionality)
(There are dependencies for development and building, but they are not acquired on installing Typethon.)
Installation from PyPI
- Install via pip:
pip install typethon
from typethon.decorators import *
@ReturnType(types=int, constraints=(lambda x: x > 0), use_arg_passing=False) # ReturnType must go before if disabling arg passing, although I have not found a reason for a user to do so
@ArgumentTypes(types={"a": int, "b": int}, constraints={"a": (lambda x: x > 0, lambda x: x < 10), "b": (lambda x: x > 0, lambda x: x < 10)}, use_arg_passing=False)
def constrained_add(a, b):
return a + b
@ArgumentTypes(use_annotations=True, constraints={"b": (lambda x: x > 0, lambda x: x <= 100)})
@ReturnType(types=type(None)) # must use NoneType to specify we actually want to restrict the return type to None, not just have no restriction
def multiplier_procedure(a: str, b: int):
print(a * b)
@ReturnType(use_annotations=True)
def silent_procedure() -> None: # the annotation parser handles None to NoneType conversion automagically here
print("Hello World!")
@Strict() # syntactic sugar for @ArgumentTypes(use_annotations=True) combined with @ReturnType(use_annotations=True), this does not accept any arguments
def strict_uppercase(a:str) -> str:
print(a)
return a.upper()
@Strict(arg_constraints={"a": (lambda x: x > 0, lambda x: x < 5)}, return_constraints=(lambda x: x > 0, lambda x: x < 10)) # Strict also accepts constraints, denoted as separate arguments
def strict_constrained_multiply(a:int) -> int:
print(a)
return a * 2
print(constrained_add(1,2))
multiplier_procedure("hello", 3)
silent_procedure()
print(strict_uppercase("hi!"))
strict_constrained_multiply(4)
- Clone the repository:
git clone https://github.com/obfuscatedgenerated/Typethon.git
- (optional but recommended) Create a virtual environment:
python -m venv env
(orpython3 -m venv env
on Linux) and then activate it:.\env\Scripts\activate
(orsource env/bin/activate
on Linux) - Install build/dev dependencies:
pip install -r requirements.txt
(orpip3 install -r requirements.txt
on Linux) - Once you've made changes, add yourself as a contributor:
python build_tool.py add_contributor <your username>
then commit and make a PR.
- VSCode users: it's recommended to use the Pylance language server as opposed to the Jedi language server as Jedi has issues with wrapper functions when displaying function signatures. You can do this by installing the Pylance extension and changing/setting the
python.languageServer
toPylance
in your settings.json file.
- Make sure all build/dev dependencies are installed:
pip install -r requirements.txt
- From your virtual environment (if you used one, otherwise just run as usual):
python build_tool.py build