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

Make create_memory_object_stream generic #495

Merged

Conversation

gschaffner
Copy link
Collaborator

@gschaffner gschaffner commented Nov 6, 2022

This supports annotating memory object streams with uninstantiable types that the current item_type hack currently does not work for, e.g.

create_memory_object_stream(item_type=Callable)
# error: No overload variant of "create_memory_object_stream" matches argument type
# "_SpecialForm"  [call-overload]

(See also the discussion around https://matrix.to/#/!JfFIjeKHlqEVmAsxYP:gitter.im/$sGb5WyohiQVFuiPdfb0GZ54h-CqN76sxeKKiCWovCdY?via=gitter.im&via=matrix.org)

@agronholm it turns out I was mistaken—due to a bug(?) in mypy, what I thought was a way to do this in a backwards compatible way isn't quite backwards compatible. While c93f3ec (note: not in this PR) is backwards-compatible at runtime (emitting a deprecation warning currently, but v3 could skip this) and backwards compatible in the sense that mypy will correctly infer types for e.g.

s0, r0 = create_memory_object_stream(item_type=float)
s1, r1 = create_memory_object_stream()

it would be a breaking change since it makes unannotated calls emit

s1, r1 = create_memory_object_stream()
# error: Need type annotation for "s1"  [var-annotated]
# error: Need type annotation for "r1"  [var-annotated]

on mypy. Note that mypy does infer a default item type of Any for s1 and r1, but it still wants a type annotation. This is not affected by --allow-any-generic or --disallow-any-generic. It appears to be a known problem, see python/mypy#3737 (comment).

Given that c93f3ec would produce spurious var-annotated on anyio 4 too, I think you are right that this has to be a breaking change in v4.

@gschaffner
Copy link
Collaborator Author

gschaffner commented Nov 6, 2022

the CI network failure seems unrelated and likely just needs a rerun

@gschaffner gschaffner force-pushed the create_memory_object_stream-typing branch from b30cbf5 to 2407e33 Compare November 7, 2022 02:23
Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good! I took the liberty of making some additional changes. Would you mind checking them to make sure I didn't do anything stupid?

@gschaffner
Copy link
Collaborator Author

thanks! your changes look great. i've also just added a couple more annotations to the docs.

@gschaffner
Copy link
Collaborator Author

i just noticed that trio implemented something similar to this a couple months after I posted this. their implementation is more complicated though—specifically, they only use an implementation like mine if TYPE_CHECKING, with a comment stating that it can't be used at runtime on < 3.9. however in my testing this implementation seems fine on 3.{7,8}.

if you don't mind, can you hold off merging this until I figure out why the trio folks did something different? I want to make sure I haven't done something wrong here.

@agronholm
Copy link
Owner

if you don't mind, can you hold off merging this until I figure out why the trio folks did something different? I want to make sure I haven't done something wrong here.

Alright, there's no rush.

@agronholm
Copy link
Owner

As this is a backwards incompatible change, it won't affect the 3.x update I am putting together.

@agronholm agronholm added this to the 4.0 milestone May 10, 2023
@agronholm
Copy link
Owner

@gschaffner I'd like to get 4.0 out in a couple weeks. Have you made progress with your investigation?

@agronholm
Copy link
Owner

I've given this another look, and I can't find any fault in it. Trio's way of doing this is more generic, but potentially also less compatible with type checkers. If there's a problem, we can sort it out during the release candidate phase.

@agronholm agronholm merged commit e7e38dc into agronholm:master Jul 11, 2023
12 checks passed
@agronholm
Copy link
Owner

Anyway, thank you for this!

@gschaffner
Copy link
Collaborator Author

gschaffner commented Aug 10, 2023

aah, i'm sorry for the delay. thank you for re-reviewing and seeing this in!

@gschaffner gschaffner deleted the create_memory_object_stream-typing branch August 10, 2023 03:41
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.

None yet

2 participants