Package: polyforce
Polyforce🔥 Enforce static typing in your codebase at runtime 🔥Documentation:https://polyforce.tarsild.io📚Source Code:https://github.com/tarsil/polyforceMotivationDuring software development we face issues where we don't know what do pass as specific parameters
or even return of the functions itself.Tools likemypyfor example, allow you to run static checking in your code and therefore
allowing to type your codebase properly butit does not enforce it when running.For those coming from hevily static typed languages likeJava,.netand many others, Python
can be overwhelming and sometimes confusing because of its versatility.Polyforcewas created to make sure you:Don't forget to type your functions and variables.Validates the typing inruntime.Don't forget thr return annotations.Wouldn't be cool to have something like this:What if my function that expects a parameter of type string, if something else is passed could
simply fail, as intended?This is wherePolyforce enters.The libraryPolyforce was designed to enforce the static typingeverywherein your code base. From functions
to parameters.It was also designed to make sure the typing is enforced at runtime.In other words, if you declare a typestringand decide to pass aninteger, it will blow throw
and intended error.The library offers two ways of implementing the solution.Via modelVia decoratorInstallation$pipinstallpolyforceHow to use itLet us see some scenarios where the conventional python is applied and then wherePolyforcecan make the whole difference for you.Conventional PythonLet us start with a simple python function.Simple functiondefmy_function(name:str):returnnameIn the normal python world, this wouldn't make any difference, and let us be honest, if you don't care
about mypy or any related tools, this will work without any issues.This will also allow this to run without any errors:my_function("Polyfactory")# returns "Polyfactory"my_function(1)# returns 1my_function(2.0)# returns 2.0The example above is 100% valid for that specific function and all values passed will be returned
equaly valid and the reson for this is because Pythondoes not enforce the static typingso
thestrdeclared for the parameternameis merely visual.With objectsclassMyClass:defmy_function(self,name:str):returnnameAnd then this will be also valid.my_class=MyClass()my_class.my_function("Polyfactory")# returns "Polyfactory"my_class.my_function(1)# returns 1my_class.my_function(2.0)# returns 2.0I believe you understand the gist of what is being referred here. So, what if there was a solution
where we actually enforce the typing at runtime? Throw some errors when something is missing from
the typing and also when the wrong type is being sent into a function?EntersPolyforcePolyforceNow, let us use the same examples used before but usingPolyforceand see what happens?Simple functionfrompolyforceimportpolycheck@polycheck()defmy_function(name:str):returnnameThe example above it will throw aReturnSignatureMissingor aMissingAnnotationbecause themissing return annotationof the function or a parameter annotation respectively.my_function("Polyforce")# Throws an exceptionThe correct way would be:frompolyforceimportpolycheck@polycheck()defmy_function(name:str)->str:returnnameSo what if now you pass a value that is not of type string?my_function(1)# Throws an exceptionThis will also throw aTypeErrorexception because you are trying to pass a typeintinto a
declared typestr.With objectsThe same level of validations are applied within class objects too.frompolyforceimportPolyModelclassMyClass(PolyModel):def__init__(self,name,age:int):...defmy_function(self,name:str):returnnameThe example above it will throw aReturnSignatureMissingand aMissingAnnotationbecause themissing return annotation for bothinitand the functionas well as the missing
types for the parameters in both.The correct way would be:frompolyforceimportPolyModelclassMyClass(PolyModel):def__init__(self,name:str,age:int)->None:...defmy_function(self,name:str)->str:returnnameThe PolyforceAs you can see, utilising the library is very simple and very easy, in fact, it was never so easy to
enforce statuc typing in python.For classes, you simply need to import thePolyModel.frompolyforceimportPolyModelAnd to use the decorator you simply can:frompolyforceimportpolycheckPolyModel vs polycheckWhen usingPolyModel, there is no need to apply thepolycheckdecorator. ThePolyModelis
smart enough to apply the same level of validations as thepolycheck.When using thePolyModelyou can use normal python as you would normally do and that meansclassmethod,staticmethodand normal functions.This like this, thepolycheckis used for all the functions that are not inside a class.LimitationsFor now,Polyforceis not looking atnative magic methods(usually start and end with double underscore).
In the future it is planned to understand those on a class level.