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

Add type annotation into untyped functions automatically #354

Merged
merged 20 commits into from Mar 31, 2021
Merged

Add type annotation into untyped functions automatically #354

merged 20 commits into from Mar 31, 2021

Conversation

ghost
Copy link

@ghost ghost commented Mar 29, 2021

Pull Request Check List

  • A news fragment is added in news/ describing what is new.
  • Test cases added for changed code.

Describe what you have changed in this PR

  • Add type annotation into untyped functions
  • It is because it reduces bug by static type checking such as mypy
  • I achieve this by automatically so that I don't missing untyped def
  • Related to Typing #261

Remarks

I believe adding type annotations into all variables is anti-pattern.

It is because,

So, I only add typing into functions.

How to use pyannotate

pyannotate is made by Guido and his team in dropbox. It's easy and powerful to use compared to monkeytype by instagram because monkeytype doesn't support pytest integration officially

Add below lines into tests/conftest.py. (I don't add this code in this PR, because I think this requires only this time.)

def pytest_collection_finish(session):
    from pyannotate_runtime import collect_types
    collect_types.init_types_collection()

@pytest.fixture(autouse=True)
def collect_types_fixture():
    from pyannotate_runtime import collect_types
    collect_types.start()
    yield
    collect_types.stop()

def pytest_sessionfinish(session, exitstatus):
    from pyannotate_runtime import collect_types
    collect_types.dump_stats("type_info.json")

Then run pytest.
It generates the typing informations of all functions what pytest run through into type_info.json.
It is based on sys.setprofile.

When you want to add typing into actual code, run below.

pdm run pyannotate -w --py3 ./**/*.py

The code will have typing automatically.

setup.cfg Outdated Show resolved Hide resolved
pyproject.toml Outdated
@@ -65,6 +67,8 @@ release = "python tasks/release.py"
test = "pytest tests/"
doc = {shell = "cd docs && mkdocs serve", help = "Start the dev server for doc preview"}
lint = "pre-commit run --all-files"
format = {shell = "black ./pdm ./tests && isort ./pdm ./tests && flake8 ./pdm ./tests"}
typecheck = "mypy ./pdm ./tests"
Copy link
Author

@ghost ghost Mar 29, 2021

Choose a reason for hiding this comment

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

Actually, it fails with 1 error currently.

ss 5

It looks not fatal, but I want to fix in ideal way possibly by another PR.

After it's solved, I want to add mypy into .pre-commit-config.yaml.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not true, it turns out there are ~500 errors to fix, which is a huge amount

Copy link
Author

Choose a reason for hiding this comment

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

It was actually blocking mypy.
After I excluded that directory -> and removed tests from mypy checking, it shows all errors correctly.

I first of all fixed "missing a type" errors in this PR.
It used to cause "missing a type" errors more than 50 functions, Now all errors of them are gone.

Copy link
Collaborator

@frostming frostming left a comment

Choose a reason for hiding this comment

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

Just a quick look, there must be many more untyped testing functions. But I don't see it necessary to annotate testing funcs, it doesn't seem conventional.

pyproject.toml Outdated Show resolved Hide resolved
setup.cfg Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
pdm/core.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
tests/test_project.py Outdated Show resolved Hide resolved
tests/test_project.py Outdated Show resolved Hide resolved
tests/test_plugin.py Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Mar 29, 2021

Oh I'm sorry for having you review many things.
I'm going to check the codes where you pointed out again.

@ghost
Copy link
Author

ghost commented Mar 30, 2021

there must be many more untyped testing functions

Sorry, I noticed that now.
For example, test_parse_local_directory_metadata is not typed, but pyannotate found it for sure.

type_info.json.log

Imade mistake when applying, so I'm going to add more fix commit in this PR.

Copy link
Collaborator

@frostming frostming left a comment

Choose a reason for hiding this comment

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

Too much to review, I suggest some of them, others should be similar

pdm/cli/commands/run.py Outdated Show resolved Hide resolved
pdm/cli/utils.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/flit.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
pdm/formats/legacy.py Outdated Show resolved Hide resolved
@frostming
Copy link
Collaborator

@ulwlu thanks for your effort in adding typing, but can we just leave tests/ untyped? Most famous projects seem to not annotate tests/ directory.

This will dramatically reduce the work to review this PR.

ulwlu added 4 commits March 30, 2021 21:05
Reason: typing in tests is not required

#354 (comment)
Added pyannotate when declaring typing into many defs, but it will not
be used in the future.
@ghost
Copy link
Author

ghost commented Mar 30, 2021

@frostming

Too much to review

Hi, I am sorry for causing you trouble by adding to many files.

I removed typing from tests, and fixed the all codes with based on what you pointed out.

I believed this would make the project more maintainable, and I found myself hooked into adding typing, but if there are parts you don't want, feel free to point out.

pdm/formats/poetry.py Outdated Show resolved Hide resolved
pdm/formats/requirements.py Outdated Show resolved Hide resolved
pdm/formats/requirements.py Outdated Show resolved Hide resolved
pdm/installers/_editable_install.py Outdated Show resolved Hide resolved
pdm/installers/synchronizers.py Outdated Show resolved Hide resolved
pdm/models/requirements.py Outdated Show resolved Hide resolved
pdm/models/specifiers.py Outdated Show resolved Hide resolved
pdm/pep582/sitecustomize.py Outdated Show resolved Hide resolved
pdm/project/config.py Outdated Show resolved Hide resolved
pdm/project/config.py Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Mar 31, 2021

@frostming
Hi, thank you for reviewing.
I fixed where you pointed out and other codes where have similar problem.

I also wrote one question.

Some codes are still using pip._vendor such as from pip._vendor.requests.models import Response, and if you'd like to move all of them into pdm.models, pleate tell me.

pdm/formats/base.py Outdated Show resolved Hide resolved
pdm/formats/requirements.py Outdated Show resolved Hide resolved
@frostming
Copy link
Collaborator

That is really huge, if you don't mind I will merge it to a temporary branch and do more modifications based on that.

@frostming frostming changed the base branch from master to refactor/typing March 31, 2021 06:37
@ghost
Copy link
Author

ghost commented Mar 31, 2021

Sure, thank you very much.

Since I see you start applying suggestions, I stop modifying here.

@frostming frostming merged commit 63b69e1 into pdm-project:refactor/typing Mar 31, 2021
@frostming
Copy link
Collaborator

Thank you, since this PR needs to be resolved first otherwise it will likely cause many merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant