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 reportUnusedImport #46937

Merged
merged 5 commits into from
Jul 10, 2022
Merged

TYP: enable reportUnusedImport #46937

merged 5 commits into from
Jul 10, 2022

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented May 4, 2022

pyright's reportUnusedImport checks py and pyi files for unused imports. If an import is explicitly marked as public (re-exported using as or in __all__) it is "used" (flake8 seems to use the same definition).

Adding unused imports to __all__ becomes messy when the file does not yet have __all__: need to list also all public symbols (constants, classes, functions).

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label May 6, 2022
@twoertwein twoertwein changed the title TYP: towards reportUnusedImport TYP: enable reportUnusedImport May 14, 2022
@twoertwein twoertwein marked this pull request as ready for review May 14, 2022 23:52
@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Jun 14, 2022
@mroeschke
Copy link
Member

This generally looked okay if you want to merge main.

@twoertwein twoertwein removed the Stale label Jul 7, 2022
@mroeschke mroeschke added this to the 1.5 milestone Jul 8, 2022
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to confirm:

  1. Nothing changed from the "public" API?
  2. In terms of code flow, we will need to ensure all "unused" imports are defined in __all__?

@twoertwein
Copy link
Member Author

__all__ as two functions: 1) it affects from xyz import * (imports only functions/classes inside __all__) and 2) in a py.typed library __all__ defines the public interface of a module (unless the module is private, starts with _).

1. Nothing changed from the "public" API?

Technically, users will import fewer modules if they use from xyz import * which could be a public API change.

2. In terms of code flow, we will need to ensure all "unused" imports are defined in `__all__`?

flake8/pyright will let you know about unused imports :) They can then either be removed, be ignored (if they have some side effects), or added to __all__ if they are meant to be public.

@mroeschke
Copy link
Member

Technically, users will import fewer modules if they use from xyz import * which could be a public API change.

Could you do a quick before/after check when from pandas import * is run? I imagine it should hopefully not change in a negative way, and maybe can be noted as a benefit that less stuff is imported :)

@twoertwein
Copy link
Member Author

Seems there is actually no differences between main and this PR when imported everything from a file:

import importlib
from pathlib import Path
import pickle

import ipdb

imported = {}
for py in Path("pandas").glob("**/*.py"):
    print(py)
    py = str(py).replace("/", ".").removesuffix(".py")
    mod = importlib.import_module("py")
    imported[py] = [x for x in dir(mod) if not x.startswith("_")]
with open("before.pickle", mode="wb") as file:
    pickle.dump(imported, file)

# ran the above first on this PR and save as after.pickle
# and then the entire script on main

after = {}
with open("after.pickle", mode="rb") as file:
    after = pickle.load(file)

for py, imports_b in imported.items():
    imports_a = set(after[py])
    imports_b = set(imports_b)
    remove = imports_b.difference(imports_a)
    added = imports_a.difference(imports_b)

    if len(remove) > 0 or len(added) > 0:
        ipdb.set_trace()  # never ended up here

@mroeschke mroeschke merged commit a4fec22 into pandas-dev:main Jul 10, 2022
@mroeschke
Copy link
Member

Thanks for double checking @twoertwein!

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
* TYP: enable reportUnusedImport

* cannot instantiate Tick+BaseOffset

* added new import to __all__

* remove references to now non-existing functions (found by pytest and reportUnsupportedDunderAll)
@twoertwein twoertwein deleted the reportUnusedImport branch September 21, 2022 15:28
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.

3 participants