Skip to content

Simple type checks in ctor called inside function not errroring #8531

Closed
@julianoes

Description

@julianoes

I'm new to using type hints in Python and stumbled upon a case where I expected mypy to error but it did not. I narrowed it down and it seems that a check in ctor is not checked when called inside a function.

Example not triggering type error:

#!/usr/bin/env python3

class TestClass:
    def __init__(self,
                 verbose: bool):
        pass

def main():
    bla = TestClass("Foo")

main()

It doesn't error as expected:

mypy script.py
Success: no issues found in 1 source file

With --script it works

mypy --strict script.py
script.py:10: error: Function is missing a return type annotation
script.py:10: note: Use "-> None" if function does not return a value
script.py:11: error: Argument 1 to "TestClass" has incompatible type "str"; expected "bool"
script.py:14: error: Call to untyped function "main" in typed context
Found 3 errors in 1 file (checked 1 source file)

Example correctly triggering type error:

#!/usr/bin/env python3

class TestClass:
    def __init__(self,
                 verbose: bool):
        pass

bla = TestClass("Foo")

It triggers the error as it probably should:

mypy script.py 
script.py:10: error: Argument 1 to "TestClass" has incompatible type "str"; expected "bool"
Found 1 error in 1 file (checked 1 source file)

And also with --strict it - of course - works:

mypy --strict script.py
script.py:8: error: Argument 1 to "TestClass" has incompatible type "str"; expected "bool"
Found 1 error in 1 file (checked 1 source file)

Versions

mypy --version
mypy 0.770
python --version
Python 3.8.2

With --strict it works:

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

        Participants

        @julianoes

        Issue actions

          Simple type checks in ctor called inside function not errroring · Issue #8531 · python/mypy