When checking mypy does not give any errors, believes that everything is in order.
If after calling FUNC2
Add line:
value_b = func1 ('11 ')
The error messages immediately go:
error: "func1" Does not return a value
Error: Argument 1 to "Func1" HAS Incompatible Type "STR"; EXPECTED "INT"
Why Mypy ignores errors inside the FUNC2 method
?
Answer 1, Authority 100%
Mypy does not check the code inside the functions that no annotations (apparently, it is done so that it is possible to add annotations gradually).
Exposure from the documentation:
no errors reported For Obviously Wrong Code
There Are Several Common Reasons Why Obviously Wrong Code Is Not Flagged AS An Error.
- The Function Containing The Error Is Not Annotated. Functions That Do Not Have Any Annotations (Neiter for Any Argument Nor for the Return Type) Are Not Type-Checked, and Even The Most Blatant Type Errs (EG
2 + 'A'
) Pass Silently. The Solutions Is To Add Annotations. WHERE THAT ISNTATIONS CAN BE CHECKED USING- Check-Untyped-Defs
.
That is, if the second function, specify the return - & gt returned value; None
or when calling myPy Add the - check-untyped-defs
flag, then the code inside the second function will start checked:
$ mypy --check-untyped-defs test.py
test.py:8: error: "Func1" Does Not Return A Value
test.py:8: Error: Argument 1 to "Func1" HAS Incompatible Type "STR"; EXPECTED "INT"
Found 2 Errors In 1 File (Checked 1 Source File)
For hardcore lovers, I can also recommend the -Strict
, when it is turned on, it will give a whole 4 errors:
$ mypy --Strict test.py
test.py:6: Error: Function Is Missing A Return Type Annotation
Test.PY: 6: Note: Use "- & gt; none" If Function Does Not Return A Value
test.py:8: error: "Func1" Does Not Return A Value
test.py:8: Error: Argument 1 to "Func1" HAS Incompatible Type "STR"; EXPECTED "INT"
test.py:12: Error: Call to Untyped Function "Func2" in Typed Context
Found 4 Errors In 1 File (Checked 1 Source File)
Turning on - Strict
equivalent to turning on the following flags (list is obtained by running Mypy -h
): - Warn-Unused-Configs
, - Disallow-Any-Generics
, - Disallow-Subclassing-Any
, - DiSallow-Untyped-Calls
, - -disallow-unhyped-defs
, - disallow-incomplete-defs
, - check-untyped-defs
, - Disallow-Untyped- Decorators
, - No-implicit-optional
, - Warn-Redundant-Casts
, - Warn-Unused-Ignores
, - Warn-Return-Any
, - No-implicit-reexport
, - Strict-Equality
.
Actually, the desired option - Check-Untyped-DEFS
originally found the addition of all these flags and launching with the removal of flags one by one.