Skip to content

gh-98852: Add extra type check to avoid a bug with parameterizing gen… - #98862

Closed
Eclips4 wants to merge 2 commits into
python:mainfrom
Eclips4:add-type-check
Closed

gh-98852: Add extra type check to avoid a bug with parameterizing gen…#98862
Eclips4 wants to merge 2 commits into
python:mainfrom
Eclips4:add-type-check

Conversation

@Eclips4

@Eclips4Eclips4 commented Oct 29, 2022

Copy link
Copy Markdown
Member

@sobolevnsobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You would need to:

  1. Add NEWS, consider using blurb CLI or online services. See news CI job logs
  2. Add tests: at least the example from the original bug report. But, I like when there are lots of tests :)

@gvanrossum
gvanrossum removed their request for review October 29, 2022 22:50
@Eclips4

Eclips4 commented Oct 30, 2022

Copy link
Copy Markdown
MemberAuthor

I find only one way how to write test for it:

classBaseCallableTests:
...
deftest_determine_new_args(self):
Callable=self.CallableT=TypeVar("T")
P=TypeVar("P")
classC(Generic[T]): ...
S=Callable[[C], P]
U=S[T]
ifself.Callableiscollections.abc.Callable:
self.assertEqual(U, Callable[[C[T]], T])
else:
self.assertEqual(U, Callable[[C], T])
...

What u think about this?
And i have one question,

fromtypingimportTypeVar, Genericfromcollections.abcimportCallablefromtypingimportCallableasTypingCallableT=TypeVar("T")
P=TypeVar("P")
Z=TypeVar("Z")
classC(Generic[Z]): ...
S=Callable[[C], P]
U=S[T]
U>collections.abc.Callable[[__main__.C[~Z]], ~T]
S_T=TypingCallable[[C], P]
U_T=S_T[T]
U_T>typing.Callable[[__main__.C], ~T]

Is it okay?

@sobolevn

Copy link
Copy Markdown
Member

It does not look right that two callables have different results.

@Eclips4

Copy link
Copy Markdown
MemberAuthor

I think, problem was solved in this PR: #98952
But, question about collections.abc.Callable and typing.Callable still relevant.

@JelleZijlstra

Copy link
Copy Markdown
Member

Closing in favor of #98952. I can't reproduce the Callable difference on current main:

Python 3.12.0a1+ (heads/pep688v2-dirty:007fdc1153, Nov 7 2022, 06:50:30) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import TypeVar, Generic
>>> from collections.abc import Callable
>>> from typing import Callable as TypingCallable
>>> >>> >>> T = TypeVar("T")
>>> P = TypeVar("P")
>>> Z = TypeVar("Z")
>>> >>> class C(Generic[Z]): ...
... >>> S = Callable[[C], P]
>>> U = S[T]
>>> U
collections.abc.Callable[[__main__.C], ~T]
>>> S_T = TypingCallable[[C], P]
>>> U_T = S_T[T]
>>> U_T
typing.Callable[[__main__.C], ~T]

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@Eclips4@sobolevn@JelleZijlstra@bedevere-bot@AlexWaygood