Skip to content
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

TYP: enable reportGeneralTypeIssues but add many exceptions #44855

Closed

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented Dec 12, 2021

reportGeneralTypeIssues is probably the most important option in pyright. This PR enables it but disables it in all files that currently have at least one incompatibility with it.

edit: merging #44251 would reduce the number of exceptions a bit. A good number of violations are because of incomplete pyi files.

Here is a list of files and how many errors they have at the moment. I will put that in an issue:

pandas/_config/localization.py 	 1
pandas/core/array_algos/masked_reductions.py 	 1
pandas/core/array_algos/quantile.py 	 1
pandas/core/array_algos/replace.py 	 1
pandas/core/arrays/_ranges.py 	 1
pandas/core/arrays/sparse/accessor.py 	 1
pandas/core/arrays/sparse/scipy_sparse.py 	 1
pandas/core/dtypes/base.py 	 1
pandas/core/indexers/utils.py 	 1
pandas/core/internals/base.py 	 1
pandas/core/ops/common.py 	 1
pandas/core/util/numba_.py 	 1
pandas/io/clipboards.py 	 1
pandas/io/json/_normalize.py 	 1
pandas/io/json/_table_schema.py 	 1
pandas/io/sas/sasreader.py 	 1
pandas/plotting/_matplotlib/style.py 	 1
pandas/core/arrays/floating.py 	 2
pandas/core/arrays/integer.py 	 2
pandas/core/arrays/numpy_.py 	 2
pandas/core/common.py 	 2
pandas/core/computation/align.py 	 2
pandas/core/computation/pytables.py 	 2
pandas/core/groupby/categorical.py 	 2
pandas/core/tools/times.py 	 2
pandas/core/util/hashing.py 	 2
pandas/io/excel/_xlsxwriter.py 	 2
pandas/io/excel/_xlwt.py 	 2
pandas/plotting/_matplotlib/tools.py 	 2
pandas/compat/pickle_compat.py 	 3
pandas/core/arrays/sparse/dtype.py 	 3
pandas/core/describe.py 	 3
pandas/core/indexes/period.py 	 3
pandas/core/internals/api.py 	 3
pandas/io/excel/_odfreader.py 	 3
pandas/io/excel/_pyxlsb.py 	 3
pandas/io/formats/info.py 	 3
pandas/io/parsers/python_parser.py 	 3
pandas/io/parsers/readers.py 	 3
pandas/io/xml.py 	 3
pandas/plotting/_matplotlib/timeseries.py 	 3
pandas/util/_test_decorators.py 	 3
pandas/conftest.py 	 4
pandas/core/arrays/boolean.py 	 4
pandas/core/arrays/masked.py 	 4
pandas/core/dtypes/common.py 	 4
pandas/core/indexes/category.py 	 4
pandas/core/reshape/concat.py 	 4
pandas/core/reshape/melt.py 	 4
pandas/core/sample.py 	 4
pandas/core/strings/object_array.py 	 4
pandas/io/excel/_odswriter.py 	 4
pandas/io/excel/_xlrd.py 	 4
pandas/io/formats/csvs.py 	 4
pandas/io/parsers/arrow_parser_wrapper.py 	 4
pandas/_config/config.py 	 5
pandas/_testing/_io.py 	 5
pandas/core/construction.py 	 5
pandas/core/indexes/extension.py 	 5
pandas/core/indexes/numeric.py 	 5
pandas/core/window/common.py 	 5
pandas/io/formats/xml.py 	 5
pandas/io/json/_json.py 	 5
pandas/io/sas/sas7bdat.py 	 5
pandas/plotting/_core.py 	 5
pandas/plotting/_matplotlib/hist.py 	 5
pandas/core/computation/expr.py 	 6
pandas/core/ops/__init__.py 	 6
pandas/core/reshape/pivot.py 	 6
pandas/core/window/ewm.py 	 6
pandas/io/common.py 	 6
pandas/io/excel/_openpyxl.py 	 6
pandas/io/formats/html.py 	 6
pandas/io/formats/style_render.py 	 6
pandas/io/parsers/c_parser_wrapper.py 	 6
pandas/core/arrays/_mixins.py 	 7
pandas/core/base.py 	 7
pandas/core/dtypes/dtypes.py 	 7
pandas/core/indexes/api.py 	 7
pandas/core/sorting.py 	 7
pandas/plotting/_matplotlib/converter.py 	 7
pandas/tseries/frequencies.py 	 7
pandas/_testing/asserters.py 	 8
pandas/core/computation/ops.py 	 8
pandas/io/excel/_base.py 	 9
pandas/_testing/__init__.py 	 10
pandas/core/groupby/ops.py 	 10
pandas/core/reshape/reshape.py 	 10
pandas/core/indexes/accessors.py 	 11
pandas/core/ops/array_ops.py 	 11
pandas/core/strings/accessor.py 	 11
pandas/core/tools/datetimes.py 	 11
pandas/core/tools/numeric.py 	 11
pandas/io/parquet.py 	 11
pandas/io/stata.py 	 11
pandas/plotting/_matplotlib/boxplot.py 	 11
pandas/plotting/_matplotlib/misc.py 	 11
pandas/core/apply.py 	 12
pandas/io/formats/excel.py 	 12
pandas/core/arrays/base.py 	 13
pandas/core/indexes/datetimes.py 	 13
pandas/io/formats/printing.py 	 13
pandas/io/html.py 	 13
pandas/plotting/_matplotlib/core.py 	 13
pandas/core/array_algos/take.py 	 14
pandas/core/arrays/string_.py 	 15
pandas/core/internals/array_manager.py 	 16
pandas/core/internals/managers.py 	 16
pandas/core/internals/blocks.py 	 17
pandas/core/internals/concat.py 	 17
pandas/tseries/holiday.py 	 17
pandas/core/arrays/timedeltas.py 	 19
pandas/core/groupby/generic.py 	 19
pandas/core/arrays/sparse/array.py 	 20
pandas/core/arrays/string_arrow.py 	 20
pandas/io/formats/format.py 	 20
pandas/io/formats/style.py 	 20
pandas/core/dtypes/cast.py 	 21
pandas/core/internals/construction.py 	 21
pandas/core/arrays/interval.py 	 22
pandas/core/nanops.py 	 22
pandas/core/groupby/grouper.py 	 23
pandas/core/indexes/datetimelike.py 	 23
pandas/core/indexes/interval.py 	 24
pandas/io/parsers/base_parser.py 	 24
pandas/core/indexing.py 	 26
pandas/core/missing.py 	 26
pandas/core/reshape/tile.py 	 26
pandas/core/arrays/period.py 	 27
pandas/core/algorithms.py 	 28
pandas/core/dtypes/concat.py 	 29
pandas/io/sql.py 	 29
pandas/core/arrays/datetimes.py 	 31
pandas/core/series.py 	 33
pandas/io/pytables.py 	 38
pandas/core/indexes/range.py 	 39
pandas/core/indexes/base.py 	 40
pandas/core/window/rolling.py 	 46
pandas/core/generic.py 	 47
pandas/core/arrays/categorical.py 	 49
pandas/core/arrays/datetimelike.py 	 50
pandas/core/indexes/multi.py 	 50
pandas/core/reshape/merge.py 	 57
pandas/core/groupby/groupby.py 	 58
pandas/core/frame.py 	 73
pandas/core/resample.py 	 74

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Dec 13, 2021
@jreback jreback added this to the 1.4 milestone Dec 14, 2021
@jreback
Copy link
Contributor

jreback commented Dec 14, 2021

ifi you can rebase (merged the other one)

@twoertwein
Copy link
Member Author

Here is also a quick and ugly script to first remove all the reportGeneralTypeIssues, run pyright, and add the needed reportGeneralTypeIssues back.

from pathlib import Path


# usage:
# first run with add = False (remove all reportGeneralTypeIssues)
# then run pyright > log (get a current list of violations)
# and then add = True (add needed exceptions)
add = False

# used to focus on only some lines of the pyright output
dir_prefix = "/path/to/the/pandas/directory"

if add:
    # read log and add exceptions
    counts = {}
    for file in sorted(
        x.split(":", 1)[0].strip()
        for x in Path("log").read_text().split("\n")
        if "error:" in x
    ):
        if not file.startswith(dir_prefix) or not Path(file).is_file():
            continue
        file = file.split("pandas/", 1)[1]
        if file not in counts:
            counts[file] = 0
        counts[file] += 1

    for file, count in sorted(counts.items(), key=lambda x: x[1]):
        path = Path(file)
        if path.is_file():
            content = path.read_text()
            if "reportGeneralTypeIssues = false" not in content:
                content = "# pyright: reportGeneralTypeIssues = false\n" + content
                path.write_text(content)
        print(file, "\t", count)
else:
    # remove all reportGeneralTypeIssues exceptions
    remove = "# pyright: reportGeneralTypeIssues = false\n"
    for file in Path("pandas").glob("**/*.py"):
        content = file.read_text()
        if remove not in content:
            continue
        file.write_text(content.replace(remove, ""))

@twoertwein
Copy link
Member Author

If this PR touched too many files, I'm happy to wait until all parts of #43744 are merged (interval.pyi #44922 and offset.pyi are missing). These PRs make many classes resolvable by mypy/pyright (currently they resolve it as Any and pyright also creates an error for things it cannot find). Unfortunately, these PRs are not guaranteed to reduce the number of errors, as they might uncover new type errors.

@jreback
Copy link
Contributor

jreback commented Dec 16, 2021

If this PR touched too many files, I'm happy to wait until all parts of #43744 are merged (interval.pyi #44922 and offset.pyi are missing). These PRs make many classes resolvable by mypy/pyright (currently they resolve it as Any and pyright also creates an error for things it cannot find). Unfortunately, these PRs are not guaranteed to reduce the number of errors, as they might uncover new type errors.

yeah let's merge those first (then rebase this and we can quickly merge)

@twoertwein
Copy link
Member Author

I will put this PR into draft mode. It will probably take some time to have all components of #43744 merged.

@twoertwein twoertwein marked this pull request as draft December 16, 2021 20:50
@jreback
Copy link
Contributor

jreback commented Dec 16, 2021

@twoertwein if its better to merge this can do that too first

@twoertwein
Copy link
Member Author

Should at last wait until mypy 0.930 is released python/mypy#11700 (comment) and integrated in the CI. Then we can also bump pyright before merging this PR. (We could already use the latest version of pyright, but we would need to clone and old version of typeshed and point pyright to it.)

@jreback jreback removed this from the 1.4 milestone Dec 23, 2021
@jbrockmendel
Copy link
Member

@twoertwein is there a viable way to break this into smaller pieces? 155 files is pretty daunting

@twoertwein
Copy link
Member Author

@twoertwein is there a viable way to break this into smaller pieces? 155 files is pretty daunting

I don't think so. Pyright does not allow as flexible configurations as mypy. I'm happy to wait (or also close this PR). One big difference between mypy and pyright (that I initially wasn't aware of) is that pyright tries to infer missing return annotations: which can uncover typing bugs.

@twoertwein
Copy link
Member Author

I think it might be best to convert this PR into an issue and then over time try to reduce the number of files that need exceptions.

@twoertwein
Copy link
Member Author

Closing this for now.

I hope that we can enable reportGeneralTypeIssues once return values are tightened with overloads (mypy assumes Any but pyright infers the type which often leads to Unions). I will focus on PRs that will slowly decrease the number of exceptions for reportGeneralTypeIssues.

@twoertwein twoertwein closed this Mar 14, 2022
@twoertwein twoertwein mentioned this pull request Mar 23, 2022
@twoertwein
Copy link
Member Author

Calling pyright with --skipunannotated would reduce it to "only" 642 errors across 111 files.

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

Successfully merging this pull request may close these issues.

None yet

3 participants