Skip to content

reveal_type() should state when surrounding function is not type checked #3629

@scode

Description

@scode

Problem

When using reveal_type(x) in a function that isn't type checked, it will report that x is of type Any even though the type may be know in other contexts. It should notify the user that the code isn't being type checked at all, so the Any is meaningless.

Example and why it's confusing

I was trying to use MyPy for the first time (count me as a noob, assume I'm a developer in a context where I don't have the ability to go learn MyPy in extreme detail). Suppose I have this code snippet as I'm trying to learn how to correctly type instance variables in a class:

class Person(object):
    def __init__(self, name):
        # type: (str) -> None
        self.name = name

def test():
    person = Person('Jane Doe')
    reveal_type(person.name)
    person.name = 5

Running type checking results in:

test.py:9: error: Revealed type is 'Any'

As a user, I had previously iterated on a bunch of other options and not understanding why the checker wasn't working. Once I got the point of using reveal_type, I took the error to confirm that "this code is being type checked, so clearly the problem isn't that" (I was worried that e.g. the entire file was being skipped).

The actual problem here is that the function test itself is just not being typed checked at all (from what I understand after having talked to someone more knowledgable than me on this), and merely adding a type signature to test causes the type checker to be enabled and result in expected output. Given:

class Person(object):
    def __init__(self, name):
        # type: (str) -> None
        self.name = name

def test():
    # type: () -> None
    person = Person('Jane Doe')
    reveal_type(person.name)
    person.name = 5

Output is now:

test.py:10: error: Revealed type is 'builtins.str'
test.py:11: error: Incompatible types in assignment (expression has type "int", variable has type "str")

It would be great if reveal_type could give me the contextually highly-likely-to-be-relevant information that type checking is just not happening for the code where I'm using it.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions