Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add additional examples about TypeIs#1814

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mikeshardmind wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
frommikeshardmind:typeis-added-guidance

Conversation

mikeshardmind
Copy link

This is a followup from discussion on discourse, and specificallythis example which was constructed with the help of many going back and forth to determine a situation that could have a parallel in real world code, and which was possibly unsound. To not overly deter users from using this when it is appropriate, counter examples of "tolerably safe" narrowing functions that operate on generic types are included.

@srittausrittau added Typing Council decisionNeeds to be approved by the Typing Council. Do not merge until approved. topic: typing specFor improving the typing spec labelsJul 21, 2024
@mikeshardmind
Copy link
Author

I'm not sure why the typing council decision label was added to this, this is purely an extended example of existing behavior to help better document the sharp edges that already exist on a typing feature and not a specification change, which does not appear to need one.

Quoting:https://github.com/python/typing-council/blob/main/README.md

The Council makes decisions in response to issues opened on this repo. Such decisions include:

If there's anything I need to do to have this proceed, please let me know.

@JelleZijlstra
Copy link
Member

This PR is to the spec, but I'm not sure why this text needs to be in the spec; it doesn't seem to specify any additional behavior. It might be a better fit for the guide on type narrowing that I added recently.

@erictraut
Copy link
Collaborator

Reviewing the proposed addition, I don't think this belongs in the typing spec. The typing spec is intended to specify how the type system works and how type checkers must behave. The proposed addition doesn't serve this purpose. It is instead directed at users ofTypeIs. It therefore belongs in thetyping guide, not the typing spec.

@mikeshardmind
Copy link
Author

Hmm. I thought placing it next to the existing example of behavior around TypeIs would make more sense, but I can retool the PR to move it to the guide, thanks for the feedback.

@srittausrittau removed Typing Council decisionNeeds to be approved by the Typing Council. Do not merge until approved. topic: typing specFor improving the typing spec labelsAug 5, 2024
@srittausrittau added the topic: documentationDocumentation-related issues and PRs labelMar 17, 2025
@@ -195,15 +195,91 @@ This behavior can be seen in the following example::
else:
reveal_type(x) # Unrelated

There are also cases beyond just mutability. In some cases, it may not be
Copy link
Member

Choose a reason for hiding this comment

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

This fits better under "Safety and soundness" below, probably as a new subheader.

The example could be made more convincing; I think it's not going to be clear to most readers why it's unsafe.

I think something like this would demonstrate it:

def takes_any_a(a: A[int | str]):    if possible_problem(a):        assert_type(a, A[int])  # OK because A[int] is a subtype of A[int | str]        if isinstance(a, B):            assert_type(a, B[int])  # A[int] & B -> B[int]            print(b.j + 1)  # boomtakes_any_a(B(i=1, j="x"))

attempt to limit type narrowing in a way that minimizes unsafety while remaining
useful, but not all safety violations can be detected.

One example of this tradeoff building off of TypeIs
Copy link
Member

Choose a reason for hiding this comment

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

This also fits better as a new subparagraph instead of as part of the header

return all(isinstance(i, int) for i in s)

However, many cases of this sort can be extracted for safe use with an
alternative construction if soundness is of a high priority,
Copy link
Member

Choose a reason for hiding this comment

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

Note this has other tradeoffs, e.g. potentially higher memory usage. For example,range(1000) is a Sequence, turning it into the equivalent tuple takes a lot more memory.

Copy link
Author

Choose a reason for hiding this comment

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

Is it really appropriate to maintain more than that there are examples of tradeoffs here and then allow people to use some basic thought from there? The runtime memory characteristics seem somewhat out of scope for typing soundness, and an exhaustive list would require further evaluation and possibly reevaluation on any sequence type added to the standard library

@mikeshardmind
Copy link
Author

mikeshardmind commentedMay 7, 2025
edited
Loading

I appreciate that you have other priorities, but if the scope of the changes after 9 months since last interaction are that you want additional tradeoffs noted (which I've noted a specific disagreement with) and slightly different organization, if the organization is that big a deal, please just provide a suggested change, merge it yourself and then amend organization, or close it, I don't want to spend the time for a continued back and forth for a minor change in organizing this months later, and you clearly have something in mind here that you could do more quickly yourself than the back and forth on it would require.

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

@JelleZijlstraJelleZijlstraJelleZijlstra left review comments

Assignees
No one assigned
Labels
topic: documentationDocumentation-related issues and PRs
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@mikeshardmind@JelleZijlstra@erictraut@srittau

[8]ページ先頭

©2009-2025 Movatter.jp