Skip to content

TYPING: Unreachable code #27396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
simonjayhawkins opened this issue Jul 15, 2019 · 10 comments
Open

TYPING: Unreachable code #27396

simonjayhawkins opened this issue Jul 15, 2019 · 10 comments
Labels
Clean Typing type annotations, mypy/pyright type checking

Comments

@simonjayhawkins
Copy link
Member

mypy 0.720 introduced --warn-unreachable http://mypy-lang.blogspot.com/2019/07/mypy-0720-released.html

$ mypy pandas --warn-unreachable
pandas\core\computation\expressions.py:68: error: Statement is unreachable
pandas\core\computation\expressions.py:123: error: Statement is unreachable
pandas\core\dtypes\dtypes.py:98: error: Statement is unreachable
pandas\core\dtypes\dtypes.py:102: error: Statement is unreachable
pandas\core\dtypes\dtypes.py:316: error: Statement is unreachable
pandas\core\dtypes\dtypes.py:541: error: Right operand of 'and' is never evaluated
pandas\core\dtypes\dtypes.py:543: error: Statement is unreachable
pandas\core\indexers.py:105: error: Statement is unreachable
pandas\core\indexers.py:233: error: Statement is unreachable
pandas\core\arrays\array_.py:230: error: Statement is unreachable
pandas\core\arrays\_ranges.py:40: error: Statement is unreachable
pandas\core\arrays\period.py:243: error: Statement is unreachable
pandas\core\indexes\interval.py:1212: error: Statement is unreachable
pandas\core\computation\pytables.py:93: error: Left operand of 'and' is always true
pandas\core\computation\pytables.py:103: error: Left operand of 'and' is always true
pandas\core\indexing.py:185: error: Left operand of 'or' is always false
pandas\core\indexing.py:246: error: Statement is unreachable
pandas\core\internals\managers.py:1491: error: Statement is unreachable
pandas\core\internals\managers.py:1505: error: Statement is unreachable
pandas\core\internals\managers.py:1517: error: Statement is unreachable
pandas\io\json\_normalize.py:255: error: Statement is unreachable
pandas\io\json\_normalize.py:284: error: Statement is unreachable
(pandas-dev)

should we include this in ci\code_checks.sh?

@simonjayhawkins simonjayhawkins added Clean Needs Discussion Requires discussion from core team before further action Typing type annotations, mypy/pyright type checking labels Jul 15, 2019
@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

I think a good idea in the long run. Are these ones now actually unreachable?

@simonjayhawkins
Copy link
Member Author

Are these ones now actually unreachable?

i've not looked in detail, but may be that the type annotation is too restrictive for those public methods.

@jbrockmendel
Copy link
Member

The computation.expressions ones are false-positives, at least one of the dtypes.dtypes is false-positive. Pretty sure the same for core.indexers

@simonjayhawkins
Copy link
Member Author

The computation.expressions ones are false-positives

Could the code or type annotations be changed to suppress the warning?

If not, it would make it difficult to add --warn-unreachable to ci\code_checks.sh

@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

The computation.expressions ones are false-positives

Not super familiar with that module but where is the value of _TEST_MODE being set? AFAICT it's only called in set_test_mode in that same module which isn't public and not called from anywhere else except outside of a test. So I think it is actually dead code?

@jbrockmendel
Copy link
Member

AFAICT it's only called in set_test_mode in that same module which isn't public and not called from anywhere else except outside of a test

It is declared global on L247, so when it is set on L248 that sets it in the module-scope too.

@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

Yea those lines are in the set_test_mode function I mentioned that doesn't get called except from the test suite though

I think there's just a lot of test code crept in there. Can take a look later and push a PR if it makes sense

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Sep 22, 2020
fangchenli added a commit to fangchenli/pandas that referenced this issue Oct 7, 2020
@fangchenli
Copy link
Member

fangchenli commented Oct 7, 2020

mypy=0.790

mypy pandas --warn-unreachable
pandas/core/arrays/_ranges.py:43: error: Statement is unreachable  [unreachable]
pandas/core/computation/parsing.py:41: error: unused 'type: ignore' comment
pandas/core/dtypes/base.py:250: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/base.py:426: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/base.py:430: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/dtypes.py:656: error: Left operand of 'and' is always true  [unreachable]
pandas/core/dtypes/dtypes.py:875: error: Left operand of 'and' is always true  [unreachable]
pandas/core/dtypes/dtypes.py:888: error: Statement is unreachable  [unreachable]
pandas/core/computation/expressions.py:67: error: Statement is unreachable  [unreachable]
pandas/core/computation/expressions.py:117: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/common.py:949: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/common.py:1227: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/common.py:1685: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/common.py:1792: error: Statement is unreachable  [unreachable]
pandas/io/common.py:217: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/io/common.py:231: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/io/common.py:244: error: Left operand of 'or' is always false  [unreachable]
pandas/io/common.py:452: error: Unsupported operand types for + ("List[Optional[str]]" and "List[str]")  [operator]
pandas/core/indexers.py:165: error: Statement is unreachable  [unreachable]
pandas/core/indexers.py:305: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/cast.py:237: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/cast.py:752: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/cast.py:1789: error: Statement is unreachable  [unreachable]
pandas/core/dtypes/cast.py:1795: error: Statement is unreachable  [unreachable]
pandas/core/missing.py:54: error: Statement is unreachable  [unreachable]
pandas/core/common.py:90: error: Statement is unreachable  [unreachable]
pandas/core/common.py:257: error: Statement is unreachable  [unreachable]
pandas/core/common.py:490: error: Statement is unreachable  [unreachable]
pandas/plotting/_matplotlib/style.py:43: error: Subclass of "List[Any]" and "str" cannot exist: would have incompatible method signatures  [unreachable]
pandas/plotting/_matplotlib/style.py:44: error: Statement is unreachable  [unreachable]
pandas/plotting/_matplotlib/style.py:59: error: Subclass of "List[Any]" and "str" cannot exist: would have incompatible method signatures  [unreachable]
pandas/plotting/_matplotlib/style.py:59: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/plotting/_matplotlib/style.py:61: error: Statement is unreachable  [unreachable]
pandas/core/apply.py:108: error: Statement is unreachable  [unreachable]
pandas/core/sorting.py:73: error: Statement is unreachable  [unreachable]
pandas/core/ops/__init__.py:232: error: Statement is unreachable  [unreachable]
pandas/core/arrays/numpy_.py:84: error: Statement is unreachable  [unreachable]
pandas/core/arrays/numpy_.py:218: error: Statement is unreachable  [unreachable]
pandas/core/arrays/period.py:766: error: Statement is unreachable  [unreachable]
pandas/core/arrays/period.py:810: error: Statement is unreachable  [unreachable]
pandas/core/arrays/period.py:1077: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/core/arrays/period.py:1084: error: If condition is always true  [unreachable]
pandas/core/arrays/datetimes.py:2250: error: Left operand of 'and' is always true  [unreachable]
pandas/core/indexes/base.py:388: error: Statement is unreachable  [unreachable]
pandas/core/indexes/base.py:898: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:519: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:528: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:576: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:581: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:622: error: Statement is unreachable  [unreachable]
pandas/core/arrays/timedeltas.py:940: error: Statement is unreachable  [unreachable]
pandas/core/arrays/interval.py:554: error: Statement is unreachable  [unreachable]
pandas/core/arrays/interval.py:1104: error: Statement is unreachable  [unreachable]
pandas/core/arrays/interval.py:1109: error: Statement is unreachable  [unreachable]
pandas/core/arrays/integer.py:383: error: Statement is unreachable  [unreachable]
pandas/core/arrays/integer.py:562: error: Statement is unreachable  [unreachable]
pandas/core/arrays/floating.py:301: error: Statement is unreachable  [unreachable]
pandas/core/arrays/categorical.py:1676: error: Statement is unreachable  [unreachable]
pandas/core/arrays/boolean.py:310: error: Statement is unreachable  [unreachable]
pandas/core/arrays/sparse/array.py:1324: error: Statement is unreachable  [unreachable]
pandas/core/internals/blocks.py:1696: error: Left operand of 'and' is always true  [unreachable]
pandas/core/internals/blocks.py:2025: error: Left operand of 'and' is always true  [unreachable]
pandas/core/internals/blocks.py:2074: error: Left operand of 'and' is always true  [unreachable]
pandas/core/internals/blocks.py:2377: error: Statement is unreachable  [unreachable]
pandas/core/indexes/numeric.py:52: error: Statement is unreachable  [unreachable]
pandas/core/indexes/range.py:722: error: Left operand of 'and' is always true  [unreachable]
pandas/core/indexes/range.py:724: error: Statement is unreachable  [unreachable]
pandas/core/indexes/range.py:733: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/core/indexes/range.py:830: error: Statement is unreachable  [unreachable]
pandas/core/indexes/range.py:834: error: Statement is unreachable  [unreachable]
pandas/core/indexes/multi.py:530: error: Statement is unreachable  [unreachable]
pandas/core/indexes/multi.py:2894: error: If condition is always true  [unreachable]
pandas/core/indexes/category.py:205: error: Left operand of 'and' is always true  [unreachable]
pandas/core/indexes/category.py:287: error: Statement is unreachable  [unreachable]
pandas/core/tools/datetimes.py:547: error: Statement is unreachable  [unreachable]
pandas/core/indexes/period.py:293: error: Statement is unreachable  [unreachable]
pandas/core/indexes/interval.py:1023: error: Statement is unreachable  [unreachable]
pandas/core/indexing.py:1830: error: Statement is unreachable  [unreachable]
pandas/core/indexing.py:2006: error: Statement is unreachable  [unreachable]
pandas/core/indexing.py:2015: error: Statement is unreachable  [unreachable]
pandas/core/indexing.py:2278: error: Statement is unreachable  [unreachable]
pandas/core/aggregation.py:483: error: Value of type variable "_LT" of "sorted" cannot be "Optional[Hashable]"  [type-var]
pandas/core/aggregation.py:606: error: Value of type variable "_LT" of "sorted" cannot be "Optional[Hashable]"  [type-var]
pandas/core/aggregation.py:634: error: Subclass of "Dict[Optional[Hashable], Any]" and "List[Any]" cannot exist: would have incompatible method signatures  [unreachable]
pandas/core/aggregation.py:635: error: Statement is unreachable  [unreachable]
pandas/core/window/rolling.py:186: error: If condition is always true  [unreachable]
pandas/core/window/rolling.py:202: error: Left operand of 'and' is always true  [unreachable]
pandas/core/window/rolling.py:1234: error: Statement is unreachable  [unreachable]
pandas/core/internals/construction.py:293: error: Statement is unreachable  [unreachable]
pandas/core/internals/construction.py:540: error: Left operand of 'and' is always true  [unreachable]
pandas/core/internals/construction.py:581: error: Statement is unreachable  [unreachable]
pandas/core/internals/construction.py:639: error: Statement is unreachable  [unreachable]
pandas/core/reshape/concat.py:342: error: Statement is unreachable  [unreachable]
pandas/io/formats/format.py:423: error: Statement is unreachable  [unreachable]
pandas/io/formats/format.py:1724: error: Statement is unreachable  [unreachable]
pandas/core/generic.py:259: error: Statement is unreachable  [unreachable]
pandas/core/generic.py:531: error: If condition in comprehension is always true  [unreachable]
pandas/core/generic.py:531: error: Subclass of "str" and "int" cannot exist: would have incompatible method signatures  [unreachable]
pandas/core/generic.py:6283: error: Statement is unreachable  [unreachable]
pandas/core/generic.py:6312: error: Statement is unreachable  [unreachable]
pandas/core/generic.py:9761: error: Subclass of "str" and "timedelta" cannot exist: would have incompatible method signatures  [unreachable]
pandas/core/series.py:939: error: Statement is unreachable  [unreachable]
pandas/core/series.py:2604: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:496: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:704: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:1793: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:1998: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:7334: error: Statement is unreachable  [unreachable]
pandas/core/frame.py:8704: error: Left operand of 'and' is always true  [unreachable]
pandas/io/parsers.py:3792: error: Statement is unreachable  [unreachable]
pandas/core/groupby/ops.py:270: error: Incompatible types in assignment (expression has type "Index", variable has type "Iterator[Any]")  [assignment]
pandas/core/groupby/ops.py:270: note: 'Index' is missing following 'Iterator' protocol member:
pandas/core/groupby/ops.py:270: note:     __next__
pandas/core/groupby/grouper.py:522: error: Statement is unreachable  [unreachable]
pandas/core/groupby/grouper.py:858: error: Statement is unreachable  [unreachable]
pandas/core/groupby/groupby.py:2782: error: Statement is unreachable  [unreachable]
pandas/io/html.py:1077: error: Subclass of "int" and "Integral" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/html.py:1077: error: Subclass of "slice" and "Integral" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/html.py:1077: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/io/excel/_base.py:356: error: Statement is unreachable  [unreachable]
pandas/core/groupby/generic.py:1046: error: Left operand of 'and' is always true  [unreachable]
pandas/core/groupby/generic.py:1203: error: Statement is unreachable  [unreachable]
pandas/core/groupby/generic.py:1232: error: If condition is always true  [unreachable]
pandas/core/reshape/pivot.py:428: error: Right operand of 'or' is never evaluated  [unreachable]
pandas/core/reshape/pivot.py:432: error: Statement is unreachable  [unreachable]
pandas/io/stata.py:1398: error: Statement is unreachable  [unreachable]
pandas/io/stata.py:1447: error: Statement is unreachable  [unreachable]
pandas/io/stata.py:1588: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:552: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:1000: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:1377: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:1983: error: Left operand of 'and' is always true  [unreachable]
pandas/io/pytables.py:3737: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:3995: error: Subclass of "int" and "List[Any]" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/pytables.py:3996: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:4540: error: Subclass of "int" and "List[Any]" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/pytables.py:4541: error: Statement is unreachable  [unreachable]
pandas/io/pytables.py:5006: error: Left operand of 'or' is always false  [unreachable]
pandas/io/pytables.py:5006: error: Subclass of "Sequence[int]" and "str" cannot exist: would have inconsistent method resolution order  [unreachable]
pandas/io/formats/excel.py:172: error: Statement is unreachable  [unreachable]
pandas/io/formats/excel.py:173: error: Subclass of "str" and "Dict[Any, Any]" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/formats/excel.py:174: error: Statement is unreachable  [unreachable]
pandas/io/excel/_odswriter.py:187: error: Statement is unreachable  [unreachable]
pandas/core/reshape/merge.py:617: error: Subclass of "bool" and "str" cannot exist: would have incompatible method signatures  [unreachable]
pandas/core/reshape/merge.py:618: error: Statement is unreachable  [unreachable]
pandas/io/formats/style.py:169: error: Left operand of 'or' is always false  [unreachable]
pandas/io/formats/style.py:1192: error: Left operand of 'or' is always false  [unreachable]
pandas/io/formats/style.py:1656: error: Statement is unreachable  [unreachable]
pandas/io/formats/style.py:1664: error: Statement is unreachable  [unreachable]
pandas/io/formats/csvs.py:314: error: Subclass of "Hashable" and "List[Any]" cannot exist: would have incompatible method signatures  [unreachable]
pandas/io/formats/csvs.py:314: error: Right operand of 'and' is never evaluated  [unreachable]
pandas/io/formats/csvs.py:315: error: Statement is unreachable  [unreachable]
pandas/plotting/_matplotlib/core.py:191: error: Statement is unreachable  [unreachable]
pandas/plotting/_matplotlib/hist.py:418: error: Statement is unreachable  [unreachable]
pandas/tseries/holiday.py:430: error: Right operand of 'or' is never evaluated  [unreachable]
pandas/tests/test_flags.py:44: error: Statement is unreachable  [unreachable]
pandas/tests/series/test_internals.py:245: error: Statement is unreachable  [unreachable]
pandas/tests/io/test_gbq.py:44: error: Left operand of 'or' is always false  [unreachable]
pandas/tests/frame/test_constructors.py:1083: error: Statement is unreachable  [unreachable]
Found 157 errors in 65 files (checked 1057 source files)

@simonjayhawkins simonjayhawkins removed the Closing Candidate May be closeable, needs more eyeballs label Dec 12, 2020
@mroeschke mroeschke removed the Needs Discussion Requires discussion from core team before further action label Jul 10, 2021
@jbrockmendel
Copy link
Member

Running mypy pandas --warn-unreachable the first three complaints i manually inspect are all false-positives. Do we have any precedent for this finding something accurately?

@MarcoGorelli
Copy link
Member

Running mypy pandas --warn-unreachable the first three complaints i manually inspect are all false-positives. Do we have any precedent for this finding something accurately?

I don't know about mypy's flag, but I tried running vulture pandas --min-confidence 100 and found

if format == "csv":
return
content = bad_encoding + b"\n" + bad_encoding
reader = pd.read_csv

which seems legit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

No branches or pull requests

6 participants