Python: type checking decorator

眉间皱痕 提交于 2019-12-07 18:23:38

问题


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.


回答1:


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 !




回答2:


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.



来源:https://stackoverflow.com/questions/36879932/python-type-checking-decorator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!