Skip to content

Fix wrong inference cache result for typing.cast calls inside functions #1981

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 1 commit into from

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Jan 28, 2023

Description

The result for the typing.cast inference tip depend could depend on the arguments passed to the parent function.
Those aren't included in the cache key which could cause the first (invalid) inference result to be returned again.

This PR adds a new parameter to inference_tip -> cached, to be able to opt out of caching for specific inference tips.
In most cases this shouldn't be needed. Therefore the default is set to True.

https://github.com/PyCQA/astroid/blob/dfd88f5edc636df80c8cabd61a6b8d6bc8746ca9/astroid/inference_tip.py#L36-L38

from typing import TypeVar, cast
T = TypeVar("T")
def ident(var: T) -> T:
    return cast(T, var)

ident(2)  #@
ident("Hello")  #@

Closes pylint-dev/pylint#8074

@codecov
Copy link

codecov bot commented Jan 28, 2023

Codecov Report

Merging #1981 (6fdd068) into main (dfd88f5) will not change coverage.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1981   +/-   ##
=======================================
  Coverage   92.65%   92.65%           
=======================================
  Files          94       94           
  Lines       10903    10903           
=======================================
  Hits        10102    10102           
  Misses        801      801           
Flag Coverage Δ
linux 92.41% <100.00%> (ø)
pypy 88.53% <100.00%> (ø)
windows 92.33% <100.00%> (ø)

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

Impacted Files Coverage Δ
astroid/brain/brain_typing.py 88.51% <ø> (ø)
astroid/inference_tip.py 96.87% <100.00%> (ø)

@cdce8p cdce8p marked this pull request as draft January 28, 2023 18:41
@cdce8p
Copy link
Member Author

cdce8p commented Jan 28, 2023

Found a potentially better solution. Just need to do some performance testing first.

@cdce8p
Copy link
Member Author

cdce8p commented Jan 28, 2023

Closing in favor of #1982

@cdce8p cdce8p closed this Jan 28, 2023
@cdce8p cdce8p deleted the fix-cast-inference-cache branch January 28, 2023 19:38
@cdce8p cdce8p added the backported Assigned once the backport is done label Jan 31, 2023
@cdce8p cdce8p removed this from the 2.13.4 milestone Jan 31, 2023
@cdce8p cdce8p removed backport maintenance/2.13.x backported Assigned once the backport is done labels Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive not-a-mapping with cast and TypeVar
2 participants