8000 Fix typing of ``_infer`` to allow a return value in the ``Generator`` by DanielNoord Β· Pull Request #1655 Β· pylint-dev/astroid Β· GitHub
[go: up one dir, main page]

Skip to content

Fix typing of _infer to allow a return value in the Generator #1655

10000
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

Merged
merged 1 commit into from
Jun 23, 2022

Conversation

DanielNoord
Copy link
Collaborator

Steps

  • Write a good description on what the PR does.

Description

Ref. #1653.

Type of Changes

Type
βœ“ πŸ”¨ Refactoring

Related Issue

@DanielNoord DanielNoord added the Maintenance Discussion or action around maintaining astroid or the dev workflow label Jun 23, 2022
@DanielNoord DanielNoord requested a review from cdce8p June 23, 2022 07:09
def infer(
self: _ProxyT, context: InferenceContext | None = None, **kwargs: Any
) -> collections.abc.Generator[_ProxyT, None, None]:
def infer( # type: ignore[return]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mypy complains about wanting a return None statement whereas pylint complains about useless-return. Who is right here?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need an explicit return at the end of a generator, so I think mypy is wrong... But they say in their doc:

You should give a statically typed function an explicit None return type even if it doesn’t return a value, as this lets mypy catch additional type errors:

So you must be explicit to help mypy it seems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that refers to:

def func(x: str):
    print(x)

Which should have a -> None as well.

Copy link
Member

Choose a reason for hiding this comment

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

I would say this is probably a mypy bug, i.e. let's leave the type: ignore[return] comment.
Mypy should only require an explicit return inside a Generator if None is not an option.

@DanielNoord Would you like to report it in the mypy repo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I'll create a small reproducer in the coming days.

Copy link
Member

Choose a reason for hiding this comment

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

Probably needs to be an option as it's a little opinionated, we were both agreeing that mypy is wrong here. As JelleZijlstra said they're consistent but another typer (or pylint) could take another decision. I don't think there's a consensus here. But the real question is what should the default value be. mypy is used a lot so probably the one mypy chose to reduce friction.

Copy link
Member
@cdce8p cdce8p Jun 24, 2022

Choose a reason for hiding this comment

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

Thanks for opening the issue @DanielNoord!

But the real question is what should the default value be. mypy is used a lot so probably the one mypy chose to reduce friction.

I would agree. Both checks (mypy and pylint) are opinionated. The only thing for us is to make sure they don't collide if a users chooses to enable either one.

Should we update pylint to allow return None for annotated functions?

Would it make sense to go one step further and disable useless-return entirely by default?

Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to go one step further and disable useless-return entirely by default?

Yeap, we don't need an option, my bad.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We should really tweak the default values of pylint.

Copy link
Member

Choose a reason for hiding this comment

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

We should really tweak the default values of pylint.

Agreed. I'm thinking about applying 3512 but there's a lot of other issues to handle and I never have the time. This is probably one of the big pain point of pylint right now though. The issue that take us a lot of time can be very niche in very specific cases sometime but default values are really freaking important. (Also invalid-name for < 3 char value. I get bitten by it for new project myself, first thing I need to do is always to disable this warning, someone trying pylint always talk to me about it, it's not a good look).

@coveralls
Copy link

Pull Request Test Coverage Report for Build 2547380225

  • 5 of 5 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.0008%) to 92.269%

Totals Coverage Status
Change from base Build 2541915235: -0.0008%
Covered Lines: 9393
Relevant Lines: 10180

πŸ’› - Coveralls

def infer(
self: _ProxyT, context: InferenceContext | None = None, **kwargs: Any
) -> collections.abc.Generator[_ProxyT, None, None]:
def infer( # type: ignore[return]
Copy link
Member

Choose a reason for hiding this comment

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

I would say this is probably a mypy bug, i.e. let's leave the type: ignore[return] comment.
Mypy should only require an explicit return inside a Generator if None is not an option.

@DanielNoord Would you like to report it in the mypy repo?

@cdce8p cdce8p added this to the 2.12.0 milestone Jun 23, 2022
@DanielNoord DanielNoord merged commit 39305fd into pylint-dev:main Jun 23, 2022
@DanielNoord DanielNoord deleted the typing-infer branch June 23, 2022 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining astroid or the dev workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0