Reader

Is it an anti-pattern to support different parameter types when using a dynamically-typed language?

| Software Engineering Stack Exchange | Default

In the Python code base we inherited there are several functions that check parameter types and try to "accommodate" different types. Example:

def process_data(arg):
    json_ = {}
    if isinstance(arg, str):
        json_ = json.loads(arg)
    elif isinstance(arg, dict):
        json_ = arg
    else:
        raise RuntimeError("bad parameter type")

In this example usually the input should be a dict; but if the user passes a JSON string, that string is parsed and handled like a dict.

To me this approach looks like an anti-pattern; but I did not find a clear name or description of this anti-pattern. Is this approach actually a good idea, or not?