portalocker icon indicating copy to clipboard operation
portalocker copied to clipboard

Type hints for utils.Lock.acquire (and __enter__) incorrect

Open clo-vis opened this issue 1 year ago • 1 comments

Lock.acquire is defined as:

def acquire(...) -> typing.IO[typing.AnyStr]:

but typing.AnyStr is a TypeVar, and "A function returning TypeVar should receive at least one argument containing the same TypeVar" (this is what mypy should have said).

See https://github.com/python/mypy/issues/16113

As a result, when I write

from portalocker import Lock

def load_binary_file(path: str) -> bytes:
    with Lock(path, mode="rb") as file:
        return file.read()  #  mypy and pyright complain

I get the errors:

mypy: Incompatible return value type (got "str", expected "bytes") pyright: Return type is unknown

clo-vis avatar Jul 24 '24 10:07 clo-vis

The stalebot 🤦🏻‍♂️

On the bright side, at least it reminds me of this issue. I'm working on getting portalocker pyright strict so this should be fixed soon :)

wolph avatar Aug 31 '24 00:08 wolph

I've just released a new version that's got 100% strict pyright checking so you shouldn't have any issues anymore :)

wolph avatar Nov 18 '24 12:11 wolph

The issue is still in the current version 3.2.0. If I run mypy on the script OP provided, I get

test.py:5: error: Incompatible return value type (got "str", expected "bytes")  [return-value]
Found 1 error in 1 file (checked 1 source file)

The reason is the same as OP explained; Lock.__enter__() and Lock.acquire() return IO[AnyStr]:

https://github.com/wolph/portalocker/blob/26aaec671b84d4272488982688bf0445eb2068fa/portalocker/utils.py#L315

https://github.com/wolph/portalocker/blob/26aaec671b84d4272488982688bf0445eb2068fa/portalocker/utils.py#L244-L249

For binary file modes, this is incorrect and should be IO[bytes] instead.

Can we please reopen this issue?

JulianJvn avatar Sep 09 '25 11:09 JulianJvn