Search code examples
pythonpython-3.xparameterstype-hinting

What's the cleanest possible way to hint that a python function accepts two arguments but only one of them must be populated?


class Authenticator:
    def __init__(api_key: str, user_pass: tuple[str, str]):
        raise NotImplemented

What' the cleanest way to write that Authenticator can be instantiated or via an api key or via a username-password tuple (but not both obviously)? I'd like to keep both parameters in the constructor.


Solution

  • I think it will be more readable if you use two different methods instead of constructor method

    class Authenticator:
    
        def auth_by_key(api_key):
            pass
    
        def auth_by_u_p(u, p):
            pass