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

Casted type from typing.cast() is ignored (no-member) #4534

Open
postlund opened this issue Jun 1, 2021 · 6 comments
Open

Casted type from typing.cast() is ignored (no-member) #4534

postlund opened this issue Jun 1, 2021 · 6 comments
Labels
Enhancement ✨ Improvement to a component High effort 🏋 Difficult solution or problem to solve Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing

Comments

@postlund
Copy link

postlund commented Jun 1, 2021

Steps to reproduce

Given a file a.py:

import wave
from typing import cast

with wave.open("dummy.wav", "wb") as handle:
    writer = cast(wave.Wave_write, handle)
    writer.setnchannels(2)
    writer.setsampwidth(2)
    writer.setframerate(44100)

Current behavior

Result of pylint a.py:

a.py:7:4: E1101: Instance of 'Wave_read' has no 'setnchannels' member; maybe 'getnchannels'? (no-member)
a.py:8:4: E1101: Instance of 'Wave_read' has no 'setsampwidth' member; maybe 'getsampwidth'? (no-member)
a.py:9:4: E1101: Instance of 'Wave_read' has no 'setframerate' member; maybe 'getframerate'? (no-member)

Expected behavior

No warnings expected. mypy does not give any errors.

pylint --version output

Result of pylint --version output:

pylint 3.0.0a3
astroid 2.5.7
Python 3.8.1 (default, Jan  8 2020, 13:13:59) 
[GCC 4.9.2]
@hippo91
Copy link
Contributor

hippo91 commented Jun 5, 2021

@postlund thanks for the report. Could you try without the call to cast? It would determine if the problem is linked to the inference of cast function or to the wave module.

@postlund
Copy link
Author

postlund commented Jun 6, 2021

@hippo91 I get the same error if I remove the cast by the looks of it.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 8, 2021
@timmartin
Copy link
Contributor

It looks to me like there's no logic currently to infer the result of the cast() function. IIUC this is something that should be solved in Astroid?

I had a go at implementing this here, I think this will need a bit of work to make it more rigorous but it seems like it works in a simple case.

@Pierre-Sassoulas
Copy link
Member

@timmartin, could you open a PR in astroid please ? Apparently bad inference in cast is not at fault for this issue, but this is still a nice feature to have :)

@timmartin
Copy link
Contributor

I will open a PR, I just wanted to do a little more review of the code first, it's probably not ready to merge.

If I'm understanding this correctly, lack of inference on typing.cast is causing this bug. It looks like if the cast was inferred correctly, Pylint would know to treat the writer as an instance of wave.Wave_write, which would presumably then have the expected methods.

@jacobtylerwalls jacobtylerwalls changed the title Does not find members when casting handle in with-statement Casted type from typing.cast() is ignored (no-member) Jun 1, 2022
@jacobtylerwalls
Copy link
Member

See discussion at pylint-dev/astroid#1076 (comment) for subtle issues with TypeVar that a proposal would need to address.

@jacobtylerwalls jacobtylerwalls added Enhancement ✨ Improvement to a component Proposal 📨 Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) High effort 🏋 Difficult solution or problem to solve typing and removed False Positive 🦟 A message is emitted but nothing is wrong with the code labels Jun 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Proposal 📨 labels Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component High effort 🏋 Difficult solution or problem to solve Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Projects
None yet
Development

No branches or pull requests

5 participants