Skip to content

Fix: Enhance W0236 checker to detect return type mismatches in overridden methods #10355

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

Closed
wants to merge 8 commits into from

Conversation

pavan-msys
Copy link

@pavan-msys pavan-msys commented Apr 27, 2025

Type of Changes

Type
🐛 Bug fix

Description

This PR improves the invalid-overridden-method (W0236) check in Pylint to detect return type mismatches when a method in a subclass overrides a method in its base class.

Previously, Pylint would not catch cases where the overridden method had an incompatible return type compared to the base method.

Changes:

  • Pylint now infers the return types of both the parent and child methods.
  • If the return types differ and are incompatible, a W0236 message is raised.
  • A new safe inference with proper exception handling (astroid.InferenceError, AttributeError) is used to avoid false positives or crashes.

@@ -1510,6 +1510,19 @@ def _check_invalid_overridden_method(
node=function_node,
)

# Handle return type compatibility check
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the try except is not needed because this is already handled by the safe_infer utility function

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback! @Julfried

I've updated the PR by removing the redundant try-except block, as the safe_infer function already handles those exceptions.

@Pierre-Sassoulas Pierre-Sassoulas added the False Negative 🦋 No message is emitted but something is wrong with the code label Apr 27, 2025
@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.0.0 milestone Apr 27, 2025
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thank you for opening a PR @pavan-msys, do you mind adding a functional test in tests/functional/ please ? The example from the issue would work, but feel free to introduce some edge case scenarios.

@pavan-msys
Copy link
Author

Hi @Pierre-Sassoulas

I'm currently working on the codebase to address edge cases and ensure all test scenarios pass successfully. I’ll provide a detailed update once the changes are complete.

Thank you!

@pavan-msys pavan-msys marked this pull request as draft April 28, 2025 15:30
@pavan-msys pavan-msys marked this pull request as ready for review April 28, 2025 15:42
@pavan-msys
Copy link
Author

Hi @Pierre-Sassoulas, @Julfried

I have updated the codebase. Could you please review the code and let me know if any changes are required?

Thanks!

@pavan-msys pavan-msys marked this pull request as draft April 28, 2025 16:13
@Pierre-Sassoulas
Copy link
Member

Hey @pavan-msys thank you for adding the functional tests. I launched the tests in the continuous integration, it seems some issue appeared. You can launch the tests locally yourself with either pytest or tox see https://pylint.readthedocs.io/en/stable/development_guide/contributor_guide/tests/launching_test.html

@pavan-msys pavan-msys marked this pull request as ready for review May 5, 2025 08:15
@pavan-msys
Copy link
Author

Hi, @Pierre-Sassoulas

I have updated the codebase to handle additional edge cases and have verified the changes through local testing.

Could you please review the changes and let me know if any further improvements are needed?

Thank you!

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thank you. It seems the functional tests are not there anymore, do you mind adding them back ? 😄

.gitignore Outdated
@@ -27,3 +27,4 @@ build-stamp
.pytest_cache/
.mypy_cache/
.benchmarks/
venv/
Copy link
Member

Choose a reason for hiding this comment

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

You should add that in your global gitignore not in each project's gitignore https://stackoverflow.com/a/7335487/2519059

@Pierre-Sassoulas
Copy link
Member

The pypy3.10 job fails everywhere, don't mind it.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@pavan-msys
Copy link
Author

Hi @Pierre-Sassoulas

Thank you for the suggestion. The test file pylint/tests/functional/i/invalid/invalid_overridden_method.py is already present and covers the necessary cases, so I didn’t add a new one.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Hey @pavan-msys, you deleted the .gitignore completely, you should have reverted the changes instead :)

There's no way that this much new code was already tested. I would expect new tests that would fail without the new code, or at the very least the sample code from the original issue.

Also, are you using a LLM to generate the code for this PR ? The style seems to be one of a LLM (with the leading comments to explain what the code does in great details). It would also explains that you're able to create a lot of code without any sample test code to run it on. If that's the case please do not use LLM like this, I could fix the problem faster by thinking about it myself and making the fix than by reviewing the output of your LLM and then telling you how to fix the slop.

@pavan-msys pavan-msys marked this pull request as draft May 5, 2025 23:46
@pavan-msys
Copy link
Author

Thank you for the feedback @Pierre-Sassoulas

I added the current code and comments to get a clearer understanding of the issue—this is not the final fix. I still need some time to investigate further and identify the root cause.

To avoid confusion, I’ll go ahead and close this PR for now. Once I have a proper solution, I’ll raise a new PR with the complete fix.

Apologies for the extra effort this may have caused, and thank you for your understanding and patience.

@pavan-msys pavan-msys closed this May 5, 2025
@pavan-msys pavan-msys deleted the 10351-issue branch May 5, 2025 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants