Skip to content

modules as protocols doesn't type-check against the protocol #13771

Closed
@asottile

Description

@asottile

Bug Report

I was really excited to see #13513 merge but trying it out seems like it doesn't actually type-check things

here's a small example

# main.py
from typing import Protocol

class P(Protocol):
    def f(self) -> str: ...

import t
t_p: P = t  # expect an error here, it does not match P
# t.py
def f() -> int:
    return 6

I expect this to error because the f inside t returns int but the protocol specifies it should return str.

additionally, I think I should also be able to write something like this, but it currently errors for an unrelated reason (even when the module matches the protocol):

from typing import Protocol

class P(Protocol):
    def f(self) -> str: ...

t: P
import t  # error: Name "t" already defined on line 6

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags: mypy main.py
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.8.10

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions