basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

Disabling `reportUnusedCallResult` for specific functions

Open jorenham opened this issue 1 year ago • 3 comments

Repost from https://github.com/microsoft/pyright/issues/8650, which got rejected:

A very common use-case of typing.assert_type is in type tests, e.g. the NumPy type tests do this.

But currently when reportUnusedCallResult is enabled, the following code

from typing import Literal, assert_type

assert_type(0, Literal[0])

results in the following pyright error:

Result of call expression is of type "Literal[0]" and is not used; assign to variable "_" if this is intentional  (reportUnusedCallResult)

https://pyright-play.net/?strict=true&reportUnusedCallResult=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2ANFOQM71kwD68CJAUJw03m4hIAKAAy0ipCpQDaIgLoBKTkA

This isn't only limited to assert_type; e.g. the return value of dict.setdefault is also often ignored, and I'm sure there are many more cases like these.

So I think that the cleanest solution would be to have a configuration option that disables reportUnusedCallResult for specific functions, which in a pyproject.toml could look like:

[tool.pyright]
# [...]
reportUnusedCallResult = true
ignoreUnusedCallResult = [
    "dict.setdefault", 
    "typing.assert_type", 
    "typing_extensions.assert_type"
]
# [...]

jorenham avatar Aug 04 '24 21:08 jorenham

i already raised #21 for assert_type but i like the idea of making it configurable

DetachHead avatar Aug 04 '24 21:08 DetachHead

i already raised #21 for assert_type but i like the idea of making it configurable

It must be true then when they say that "great minds think alike" 😉 .

jorenham avatar Aug 04 '24 21:08 jorenham

I would very much like to see 'reportUnusedCallResult' enabled by a decorator, as a way of gently introducing this check to an existing code base.

finite-state-machine avatar Sep 18 '24 18:09 finite-state-machine

another example of this, list.pop(), was mentioned by @denballakh

jorenham avatar Jan 02 '25 10:01 jorenham

There is file.write(...) as well. Result is almost never used.

denballakh avatar Jan 02 '25 11:01 denballakh

I suppose it would be good to have a decorator for annotating first party code, and a config option for third party code

KotlinIsland avatar Jan 02 '25 14:01 KotlinIsland

I suppose it would be good to have a decorator for annotating first party code, and a config option for third party code

and 2nd party code...?

jorenham avatar Jan 02 '25 14:01 jorenham