caterinaurban / Typpete

34 stars 6 forks source link

Wrong return type inferred for constructors #44

Closed marcoeilers closed 6 years ago

marcoeilers commented 6 years ago

In Mypy, constructor return types are always None (https://github.com/python/mypy/issues/604), but we seem to infer the type of the class as the return type instead? Or maybe we infer arbitrary types and I just happened to get the type of the class as the return type? Either way, it should be None instead.

marcoeilers commented 6 years ago

Actually, this might be an instance of a larger problem. I think I remember seeing that we infer a return type that is not None for functions that don't actually return anything. I think we shouldn't do that, they should get type None.

mostafa-abdullah commented 6 years ago

The return type of a function with no return is defined as a super type of None. The reason for this was to make this program typeable:

class A:
    def f(self):
        pass

class B(A):
    def f(self):
        return 1

If the method f in class A is restricted to return None, the program will not be typeable due to the variance rules imposed by overriding this method in class B

caterinaurban commented 6 years ago

Can’t we support the @abstractmethod decorator instead?

mostafa-abdullah commented 6 years ago

Done here

Now you can declare a class as abstract by setting its metaclass to ABCMeta (imported from abc module), and declare a method as abstract by decorating it with @abstractmethod. You now get unsat if you try to instantiate an abstract class.

mostafa-abdullah commented 6 years ago

I also modified some unit tests to adapt to this change.

caterinaurban commented 6 years ago

Don't we need to also modify the projects like imp?

mostafa-abdullah commented 6 years ago

I did