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

tqdm: Improve wrapattr #13361

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

tqdm: Improve wrapattr #13361

wants to merge 2 commits into from

Conversation

MBradbury
Copy link

The stub for tqdm.wrapattr does not type the stream parameter and the generator is marked as Incomplete. This PR fixes both.

Example of error from Pylance that is fixed:

Type of "wrapattr" is partially unknown
  Type of "wrapattr" is "(stream: Unknown, method: Literal['read', 'write'], total: float | None = None, bytes: bool = True, **tqdm_kwargs: Unknown) -> _GeneratorContextManager[Any]
file_size = os.stat("source.txt").st_size

# Either wrap the dest
with open("source.txt", "rb") as source, open("dest.txt", "wb") as dest:
    with tqdm.wrapattr(dest, "write", total=member.file_size, desc="Copying source.txt to dest.txt") as dest_tqdm:
        shutil.copyfileobj(source, dest_tqdm)

# or the source
with open("source.txt", "rb") as source, open("dest.txt", "wb") as dest:
    with tqdm.wrapattr(source, "read", total=member.file_size, desc="Copying source.txt to dest.txt") as source_tqdm:
        shutil.copyfileobj(source_tqdm, dest)

Also:

  • bytes is marked as just bool as this seems to better represent the tqdm implementation
  • **tqdm_kwargs is marked as Incomplete

Pylance is not happy with the dest_tqdm argument to shutil.copyfileobj and reports this error, any suggestions on how to address would be appreciated.

Argument of type "SupportsWrite[ReadableBuffer]" cannot be assigned to parameter "fdst" of type "SupportsWrite[AnyStr@copyfileobj]" in function "copyfileobj"
  "SupportsWrite[ReadableBuffer]" is not assignable to "SupportsWrite[AnyStr@copyfileobj]"
    Type parameter "_T_contra@SupportsWrite" is contravariant, but "ReadableBuffer" is not a supertype of "AnyStr@copyfileobj"
      Type "ReadableBuffer" is not assignable to constrained type variable "AnyStr"

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Jan 3, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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

Successfully merging this pull request may close these issues.

1 participant