Skip to content

Sibling subclasses (without stubs) pass for each other? #3987

Closed
@mbforbes

Description

@mbforbes

In what is perhaps a fool's errand, I'm hoping to use mypy to help typecheck code written using a big library without stubs (pytorch).

mypy seems to know something about the types of the variables, but not quite enough to be helpful. I'm hoping to understand why the below happens and if there's anything I can do about it.

Code:

import torch

i = torch.IntTensor()
f = torch.FloatTensor()
reveal_type(i)
reveal_type(f)

def foo(i: torch.IntTensor, f: torch.FloatTensor) -> None:
    pass

# these typecheck as expected
foo(i, f)  # OK
foo(1, 2)  # error

# these typecheck OK, but I think should error
foo(i, i)
foo(f, f)

Output of mypy --follow-imports=silent:

5: error: Revealed type is 'torch.IntTensor'
6: error: Revealed type is 'torch.FloatTensor'
13: error: Argument 1 to "foo" has incompatible type "int"; expected "IntTensor"
13: error: Argument 2 to "foo" has incompatible type "int"; expected "FloatTensor"

I am sorry if this is because of my confusion with python or mypy in general.


More info:

The definitions of the above classes:

class FloatTensor(_C.FloatTensorBase, _TensorBase):

    def is_signed(self):
        return True

    @classmethod
    def storage_type(cls):
        return FloatStorage

class IntTensor(_C.IntTensorBase, _TensorBase):

    def is_signed(self):
        return True

    @classmethod
    def storage_type(cls):
        return IntStorage

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions