daniel451
daniel451

Reputation: 11002

Python: type checking decorator

I've built a type checking decorator (with wraps):

def accepts_func(*types):
    """ 
    top-level decoration, consumes parameters
    """

    def decorator(func):
        """ 
        actual decorator function, consumes the input function
        """

        @wraps(func)
        def check_accepts(*args):
            """ 
            actual wrapper which does some magic type-checking
            """

            # check if length of args matches length of specified types
            assert len(args) == len(types), "{} arguments were passed to func '{}', but only {} " \
                                            "types were passed to decorator '@accepts_func'" \
                .format(len(args), func.__name__, len(types))

            # check types of arguments
            for i, arg, typecheck in izip(range(1, len(args)+1), args, types):
                assert isinstance(arg, typecheck), "type checking: argument #{} was expected to be '{}' but is '{}'" \
                    .format(i, typecheck, type(arg))

            return func(*args)

        return check_accepts

    return decorator

You can pass as many types as you want and it checks if the types of the parameters passed to func match the ones that were "hardcoded" in @accepts_func(param_type1, param_type2, ...):

@accepts_func(int, str)
sample_func(arg1, arg2):
    ...does something...

It works without any problems so far.


However, since I am not a Python "guru" I would like to know if my solution is appropriate for "bigger" projects?

Are there any downsides in my solution? E.g. like performance issues, uncaught errors in edge cases, and stuff?

Is there a way to improve my solution? Do better, more "pythonic" solutions exist?

Note: I'm not type checking every function in my project, just the ones there I think I really need type safety. The project runs on a server, hence, errors thrown appear in logs and are not visible to the user.

Upvotes: 5

Views: 6733

Answers (3)

Innovations Anonymous
Innovations Anonymous

Reputation: 134

I was looking for a typechecking decorator, and couldn't find anything that's maintained in the modern era. This thread comes up toward the top of the search results. After trying and failing to implement my own typechecking decorator using the code here, I eventually stumbled upon typeguard.

Upvotes: 1

gnicholas
gnicholas

Reputation: 2077

If you want type checking use python 3.5 and its typing module which has support for built in type-hinting.

http://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/

EDIT:

As a warning to the reader. Type hinting in a language like python can be useful but is also a pain. Lots of python APIs are highly polymorphic, accepting many different types of different arguments and optional arguments. The type signatures on these functions are gnarly and annotating them is not helpful at all. But for simple functions that take and return simple types type hinting can only help improve clarity.

Upvotes: -2

User2342342351
User2342342351

Reputation: 2174

I would actually discourage to typecheck input variables. Performance aside, Python is a dynamically typed language and in some cases (testing, for instance) you would need to pass an object that implement some attributes of the object you initially planned to encourted and, that will work fine with your code.

A simple example:

class fake_str:
    def __init__(self, string):
        self.string = string

    def __str__(self):
        return self.string

string = fake_str('test')

isinstance(string, str) # False
string # 'test'

Why would you not accept something that is working ?

Just allow compatible objects to work with your code.

Easier to ask for forgiveness than permission !

Upvotes: 3

Related Questions