Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting values that are multiple types?

My question is different than the title implies (I don't know how to summarize the question so I'm having a hard time googling).

I do not want a Union type. Union[A, B] says that the type can be either of type A, or of type B.

I need to the opposite. I want it to mean that it is both type A and B, which is possible in python because of mixins.

That is, I need to type hint a function such that I know the arguments passed will be of a class that has both A and B as parents, as my function uses methods from both mixins. A Union type hint allows passing something that has A without B which should not be allowed.

Example

from typing import Union

class A(object):
    def a(self):
        return True

class B(object):
    def b(self):
        return True

class C(A, B):
    pass

def foo(d: Union[A,B]) -> bool: #need something other than Union! 
    print(d.a() and d.b())

I need d to be an A and a B. But currently it allows me to send things that are A without being B, and errors when it tries to call the non-existent function

>>> foo(A())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
AttributeError: 'A' object has no attribute 'b'
>>> foo(B())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
AttributeError: 'B' object has no attribute 'a'
>>> foo(C())
True

Further I'd like to note, that the type can't just be d: C. This is because there are many classes that have A and B, and it would be a ridiculously long Union that would need to be maintained.

like image 555
Cruncher Avatar asked Sep 16 '19 18:09

Cruncher


People also ask

How do you specify multiple return types in Python?

To return multiple values from a function in Python, return a tuple of values. As you may know, a tuple is a group of comma-separated values. You can create a tuple with or without parenthesis. To access/store the multiple values returned by a function, use tuple destructuring.

Can a Python function have multiple return types?

A function is not restricted to return a variable, it can return zero, one, two or more values. This is the default property of python to return multiple values/variables which is not available in many other programming languages like C++ or Java.

Are type hints Pythonic?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. The name: str syntax indicates the name argument should be of type str . The -> syntax indicates the greet() function will return a string.

What is the point of type hinting?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.


1 Answers

You can use next OOP approach.

  1. Create interface - it's abstract class in python, which can show methods, which implements concrete classes. Example:

    from abc import ABC, abstractmethod
    
    class MyAB(ABC):
        @abstractmethod
        def a(self):
            pass
    
        @abstractmethod
        def b(self):
            pass
    
    
    class A(object):
        def a(self):
            return True
    
    
    class B(object):
        def b(self):
            return True
    
    
    class ConcreteClass(MyAB, A, B):
        pass
    
    
    def foo(d: MyAB):
        print(d.a() and d.b())
    
    
    c = ConcreteClass()
    
    foo(c)
    
  1. You say - parameter d in function foo can be able using two methods a and b. Thats all that you need.
like image 111
Denis Sukhoverkhov Avatar answered Oct 19 '22 22:10

Denis Sukhoverkhov