Enable use-before-def error code by default#14166
Conversation
This comment has been minimized.
This comment has been minimized.
|
Looks like a lot of mypy_primer failures are from partially_defined not recognizing imports. I'll try to make it handle imports soon. |
# Conflicts: # mypy/partially_defined.py
This comment has been minimized.
This comment has been minimized.
|
It looks like this check is triggering some hits in |
This comment has been minimized.
This comment has been minimized.
Yep, good idea! |
This comment has been minimized.
This comment has been minimized.
|
It looks like for i in range(2):
if i == 0:
foo = 1
else:
bar = fooThe partially-defined check can't understand that |
Yep, agreed. We now have two checks in place: use-before-def and partially-defined. Potentially, we can call the example you listed as partially-defined and accept that it will have a higher false-positive rate. That way use-before-def will have a lower false-positive rate and makes more sense to enable it by default/have users not disable it. |
I think that would be a good idea, if it's not too difficult to do. Just the difference in error message is quite significant imo — (Thanks for all the work you've been doing on this btw — this is a great feature!) |
This comment has been minimized.
This comment has been minimized.
|
#14175 has the fixes from this PR but doesn't enable use-before-def by default. |
|
After #14175 is merged, we will have the following false-positives causes for
As Alex and I discussed above, solving (2) and (3) is difficult/not feasible to handle correctly in the near term. I'll probably send a PR to move detection of these to the I'm not sure what we can do for (4). Most of these seem to be when using @AlexWaygood @JukkaL @ilevkivskyi do you have thoughts? |
|
#14176 moves the false positives from (2) and (3) to partially defined check. |
|
Assuming tests pass, all that's remaining should be:
|
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/plugins.py:59: error: Name "result" is used before definition [used-before-def]
kornia (https://github.com/kornia/kornia)
+ kornia/augmentation/container/utils.py:289: error: Name "_param" is used before definition [used-before-def]
+ kornia/augmentation/container/utils.py:299: error: Name "geo_param" is used before definition [used-before-def]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/namespace.py:159: error: Name "range" is used before definition [used-before-def]
|
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/plugins.py:59: error: Name "result" is used before definition [used-before-def]
kornia (https://github.com/kornia/kornia)
+ kornia/augmentation/container/utils.py:289: error: Name "_param" is used before definition [used-before-def]
+ kornia/augmentation/container/utils.py:299: error: Name "geo_param" is used before definition [used-before-def]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/namespace.py:159: error: Name "range" is used before definition [used-before-def]
|
This one would occur because we set the errors module with global options, instead of per-module override ones. It only mattered for checks that happened after the partially undefined checks, which (I believe) is only the unused `type: ignore` checks. This was discovered when updating tests for #14166. I've also cleaned up the function signature a little.
These errors are already reported by the (new) semantic analyzer. I've discovered this while updating unit tests for new semanal in #14166. Tests are included.
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/plugins.py:59: error: Name "result" is used before definition [used-before-def]
kornia (https://github.com/kornia/kornia)
+ kornia/augmentation/container/utils.py:289: error: Name "_param" is used before definition [used-before-def]
+ kornia/augmentation/container/utils.py:299: error: Name "geo_param" is used before definition [used-before-def]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/namespace.py:159: error: Name "range" is used before definition [used-before-def]
|
# Conflicts: # test-data/unit/check-kwargs.test
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/plugins.py:59: error: Name "result" is used before definition [used-before-def]
kornia (https://github.com/kornia/kornia)
+ kornia/augmentation/container/utils.py:289: error: Name "_param" is used before definition [used-before-def]
+ kornia/augmentation/container/utils.py:299: error: Name "geo_param" is used before definition [used-before-def]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/namespace.py:159: error: Name "range" is used before definition [used-before-def]
|
JukkaL
left a comment
There was a problem hiding this comment.
Looks good to me. It's exciting to have mypy finally detect undefined variables!
|
Is this not a false positive? |
Yes. We decided that it is still acceptable to merge PR, since there is only one false positive and this also finds real errors. Created #14476 to track the false positive. |
This enables the error code added in #14163.
We aren't sure if it will cause too many problems. We can see what the mypy primer outputs as well.