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

Check for call context in PartialFunction.infer_call_result #2098

Merged
merged 4 commits into from
May 19, 2023

Conversation

nickdrozd
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix

|

Description

Check a context's call context. Fixes four Mypy errors.

@codecov
Copy link

codecov bot commented Apr 4, 2023

Codecov Report

Merging #2098 (8faeb47) into main (b4bb2d9) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2098      +/-   ##
==========================================
- Coverage   92.76%   92.76%   -0.01%     
==========================================
  Files          94       94              
  Lines       11016    11011       -5     
==========================================
- Hits        10219    10214       -5     
  Misses        797      797              
Flag Coverage Ξ”
linux 92.52% <100.00%> (-0.01%) ⬇️
pypy 88.05% <100.00%> (-0.01%) ⬇️
windows 92.31% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Ξ”
astroid/objects.py 94.04% <100.00%> (+0.03%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Shouldn't this be an assert? We're inferring a call so callcontext should probably be set

@nickdrozd
Copy link
Contributor Author

If that's the case then maybe that information should be encoded at the type level. Maybe CallContext could be reworked to be a special type of InferenceContext? I don't personally have any confidence that the call context will always be defined here, but I don't understand the inference apparatus very well.

@DanielNoord
Copy link
Collaborator

I don't know, but it makes no sense to change the logic if we are inadvertently introducing bugs with it. This changes the behaviour of the function and feels a bit like moving Chesterton's Fence.
I think a deepdive into infer_call_result would be really worthwhile but it should probably start from the top rather than doing individual changes in single definitions of it.

DanielNoord
DanielNoord previously approved these changes Apr 4, 2023
astroid/objects.py Outdated Show resolved Hide resolved
nickdrozd and others added 2 commits April 4, 2023 18:25
Co-authored-by: DaniΓ«l van Noord <13665637+DanielNoord@users.noreply.github.com>
@nickdrozd nickdrozd requested a review from DanielNoord April 5, 2023 13:29
@DanielNoord DanielNoord added this to the 2.16.0 milestone Apr 7, 2023
@DanielNoord DanielNoord added the Enhancement ✨ Improvement to a component label Apr 7, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.16.0, 3.0 Apr 24, 2023
@DanielNoord
Copy link
Collaborator

I have just cleaned up the signature of infer_call_result. However, if not had any insight into whether callcontext is always set.

@cdce8p cdce8p modified the milestones: 3.0.0a1, 3.0.0a2 Apr 25, 2023
@nickdrozd
Copy link
Contributor Author

I have just cleaned up the signature of infer_call_result. However, if not had any insight into whether callcontext is always set.

So revert back to checking, rather than asserting?

@DanielNoord
Copy link
Collaborator

I don't know. Asserting might very well be correct! Or we might want a subclass where the attribute has always been set?

@cdce8p cdce8p modified the milestones: 3.0.0a2, 3.0.0a3 Apr 25, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.0.0a3, 3.0.0a4 May 14, 2023
@jacobtylerwalls jacobtylerwalls changed the title Check for call context Check for call context in PartialFunction.infer_call_result May 19, 2023
@jacobtylerwalls
Copy link
Member

I think this change makes sense for now. Whether CallContext must be part of the context object depends on whether or not the call is to a property. Here, this PR is only altering PartialFunction.infer_call_result, in a clear case where the code would have crashed if the assumption CallContext was always set was violated.

Here is where the CallContext is usually set if you are going through infer_call:

if hasattr(callee, "infer_call_result"):
callcontext.callcontext = CallContext(
args=self.args, keywords=self.keywords, callee=callee
)
yield from callee.infer_call_result(caller=self, context=callcontext)

Let's just get the mypy errors fixed πŸ‘

@jacobtylerwalls jacobtylerwalls merged commit feafcb8 into pylint-dev:main May 19, 2023
@jacobtylerwalls jacobtylerwalls removed the request for review from DanielNoord May 19, 2023 12:38
@DanielNoord
Copy link
Collaborator

Let's just get the mypy errors fixed +1

Although I heavily agree with this I'm also wary of adding technical debt by adding asserts and code that we don't fully understand. In this case it might be fine but I do think we should be re-evaluating on a case by case basis and not just add them everywhere.

@jacobtylerwalls
Copy link
Member

Agree! (I walked myself through the infer_call_result patterns before approving πŸ“š .)

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

Successfully merging this pull request may close these issues.

5 participants