GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PEP 675: List the `str` methods that preserve literal types by pradeep90 · Pull Request #2282 · python/peps

Conversation

@pradeep90

@pradeep90 pradeep90 commented Jan 27, 2022

Copy link
Copy Markdown
Contributor

cc @gbleaney

@JelleZijlstra

Mostly looking for feedback on the two design choices we need to make:

  1. Do we restrict literal-preserving str methods to (a) just the four mentioned in "Type Inference" or (b) to the full set of str methods?
  2. Do we (a) overload the typeshed stubs for those methods or (b) leave the str stub as is and ask type checkers to hardcode literal behavior?

We are leaning towards 1b and 2a. Let us know your thoughts. We have also offered to change it if the Steering Council feels strongly.

Also, I recall that, as a typeshed maintainer, you wanted us to give clear recommendations for future typeshed changes. Could you clarify the questions we need to address?

Finally, we'll change the name from Literal[str] in a PR after the above stuff is resolved.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pradeep90
pradeep90 requested a review from JelleZijlstra as a code owner January 27, 2022 02:02
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jan 27, 2022
JelleZijlstra
JelleZijlstra requested changes Jan 27, 2022
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'd prefer 2a. Documenting the decision in typeshed makes it more discoverable than a set-in-stone list in a PEP that every type checker has to copy.

Question 1 is related to my previous query about typeshed. Basically, as a typeshed maintainer I want to know when I should accept a PR that adds Literal[str] to a method. Perhaps we could say something like "In typeshed, any pure function returning a string may/should be annotated as returning Literal[str] when the arguments are literals.", with some suitable definition of "pure".

For example, if someone wants us to add a Literal[str] overload for https://docs.python.org/3/library/fnmatch.html#fnmatch.translate (random example from git grep 'def .*: str.*-> str'), what should we say?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pradeep90 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Outdated
checkers may need to special-case ``str`` so that error messages are
understandable for users.

If the Steering Council is opposed to this typeshed stub change, we

@JelleZijlstra JelleZijlstra Jan 27, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This feels too low-level for the SC to decide, unless you have concrete indications that it's something they're concerned about. I'd prefer if the PEP takes a concrete stance, otherwise it just becomes something that can drag out the discussion. (I don't want another PEP 646.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pradeep90 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@pradeep90 pradeep90 Jan 27, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'd prefer if the PEP takes a concrete stance

Makes sense. I'll add the concrete stance we've agreed on and remove this offer.

(I don't want another PEP 646.)

Too soon, Jelle. Too soon :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CAM-Gerlach reacted with laugh emoji
All reactions
  • 😄 1 reaction
Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@CAM-Gerlach CAM-Gerlach changed the title PEP 675: List the str methods that preserve literal types. PEP 675: List the str methods that preserve literal types Jan 27, 2022
@CAM-Gerlach

CAM-Gerlach commented Jan 27, 2022

Copy link
Copy Markdown
Member

Just to note, as others have raised it when I've done the same thing—generally speaking major substantive discussion of PEP contents, particularly high level feedback on design choices, is "supposed" to happen on the PEP's discussion thread, or (if necessary for more granular feedback before being discussed by a wider audience), a PR on the author's fork (or elsewhere). PRs on the main PEPs repo are "supposed" to only be made once the changes are ready for PEP editor review and merging, rather than a place to iterate.

(Also, minor nit, but PR titles, like all titles in English [and Git commit summary messages], don't end with a period)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Jan 27, 2022

Copy link
Copy Markdown
Member

I think discussion among the PEP authors and sponsors for a pending PR is fine, especially when the author directly asks for feedback.

CAM-Gerlach reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CAM-Gerlach
CAM-Gerlach requested changes Jan 27, 2022
View reviewed changes

@CAM-Gerlach CAM-Gerlach left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fix the syntax error causing a build failure and a few other RST syntax issues

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Outdated
Comment on lines +545 to +546
As PEP 586 `mentions
<https://www.python.org/dev/peps/pep-0586/#backwards-compatibility>`_,

@CAM-Gerlach CAM-Gerlach Jan 27, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
As PEP 586 `mentions
<https://www.python.org/dev/peps/pep-0586/#backwards-compatibility>`_,
As PEP 586 :pep:`mentions <586#backwards-compatibility>`,

Is there a reason this was reverted? It is simpler and more reliable to use :pep: to refer to PEPs rather than hardcoding the full absolute URL, and will allow us to do things like show the PEP's title in the titletext and other things in the future.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AA-Turner AA-Turner Jan 27, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@CAM-Gerlach can we add a lint check for this? It could probably look for python.org/dev/peps with a whitelist of ones we don't want to change [yet] (a few in 1,12,287 from memory).

A

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CAM-Gerlach reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@pradeep90 pradeep90 Jan 27, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is there a reason this was reverted? It is simpler and more reliable to use :pep: to refer to PEPs rather than hardcoding the full absolute URL, and will allow us to do things like show the PEP's title in the titletext and other things in the future

Huh. The official PEP page renders the :pep: link correctly but the GitHub "View file" Markdown preview doesn't. I was going by the latter. Will revert back to the :pep: version.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@CAM-Gerlach CAM-Gerlach Jan 27, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

can we add a lint check for this? It could probably look for python.org/dev/peps with a whitelist of ones we don't want to change [yet] (a few in 1,12,287 from memory).

Good idea; in work. If/when PEP 676 is accepted and we transition to the Sphinx-based infra, we could also add one for hardcoded links to PEP supplementary files, instead of using :download: (or another appropriate role)—unless you have a good way to do this now that works across both the legacy and new build systems and doesn't break when changing the output mode (files/directories/etc).

Huh. The official PEP page renders the :pep: link correctly but the GitHub "View file" Markdown preview doesn't. I was going by the latter. Will revert back to the :pep: version.

The GitHub Markdown preview only has very limited compatibility with RST syntax and should never be relied upon; we will have rendered previews on PRs soon assuming PEP 676 is accepted (or possibly before, depending on when @AA-Turner adds it). Until then, please follow the instructions in the Readme/Contributing guide (basically, install the requirements.txt then run the build.py script for the new build system or the pep2html.py one for the old one to build a preview.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pradeep90 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

pradeep90 added 2 commits January 27, 2022 13:42
@pradeep90
PEP 675: type checker vs dedicated security linter
d3c7991
@pradeep90
PEP 675: describe logging format string injection
d541837
@CAM-Gerlach

CAM-Gerlach commented Jan 27, 2022

Copy link
Copy Markdown
Member

Hey @pradeep90 I see all the suggestions were marked as resolved, but they apparently didn't get applied—any idea what's up?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pradeep90

pradeep90 commented Jan 27, 2022

Copy link
Copy Markdown
Contributor Author

@CAM-Gerlach I'm working through them and marking them as resolved. Will push all my commits and request re-review when I'm done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CAM-Gerlach

CAM-Gerlach commented Jan 27, 2022

Copy link
Copy Markdown
Member

FYI, if you go to the files tab of the PR, you can click "Add to batch" on each suggestion and then use the "Commit" button at the top to commit them, preferably with an informative message. This commits the suggestions directly to your branch (which you then can pull, etc. locally) in one click, marks and auto-resolves the linked review comments, and gives the suggestion contributor attribution (which isn't a bit deal to me, but matters to some people). This is much easier and faster than applying them manually, avoiding extra work for you and the chance of making a mistake or missing a suggestion, and is also easier for reviewers, as its clear what suggestions were applied and in what commits.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pradeep90 added 5 commits January 27, 2022 15:42
@pradeep90
PEP 675: add exhaustive list of str methods to update
4c60f3b
These methods will need to have an overload for the `Literal[str]` type.
@pradeep90
PEP 675: address wording nits
8f3ff0e
@pradeep90
PEP 675: take concrete stance on typeshed
485f0d7
@pradeep90
PEP 675: recommend how to use Literal[str] in stubs
6aa66e3
@pradeep90
PEP 675: thank CAM Gerlach
d7bece6
@pradeep90
pradeep90 force-pushed the literal-strings branch from 1564365 to d7bece6 Compare January 27, 2022 23:45
JelleZijlstra
JelleZijlstra reviewed Jan 27, 2022
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks! Noticed one typo, I'll push the fix

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pep-0675.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra
Update pep-0675.rst
fc489b7
CAM-Gerlach
CAM-Gerlach approved these changes Jan 28, 2022
View reviewed changes

@CAM-Gerlach CAM-Gerlach left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM, thanks @pradeep90!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pradeep90
PEP 675: fix assorted wording problems
501cc40
@pradeep90

pradeep90 commented Jan 28, 2022

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra I did a full pass of the PEP and fixed a bunch of issues in the latest commit.

CAM-Gerlach reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CAM-Gerlach CAM-Gerlach mentioned this pull request Jan 28, 2022
Upgrade lint infra & add a few more conservative built-in checks #2286
Merged
8 tasks done
@pradeep90

pradeep90 commented Feb 1, 2022

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra gentle reminder in case this slipped off your radar.

(Working on the name change PR now.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra approved these changes Feb 1, 2022
View reviewed changes
@JelleZijlstra
JelleZijlstra merged commit 6f11197 into python:main Feb 1, 2022
@CAM-Gerlach CAM-Gerlach mentioned this pull request Feb 1, 2022
PEP 12: Update guidance on linking PEPs/RFCs per recent changes, and add check for it #2291
Merged
3 tasks done
cdce8p
cdce8p reviewed Feb 16, 2022
View reviewed changes
Comment thread pep-0675.rst
Comment on lines +969 to +973
# after
@overload
def capitalize(self: Literal[str]) -> Literal[str]: ...
@overload
def capitalize(self) -> str: ...

@cdce8p cdce8p Feb 16, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Don't know if that's the right place for it to ask.

Instead of overloads, wouldn't it also work / be better to use a bound TypeVar or the new Self type? The way I understand the PEP, LiteralString should be defined as subtype of str within typeshed.

From the PEP:

Rationale

[...] LiteralString is the "supertype" of all literal string types.
In effect, this PEP just introduces a type in the type hierarchy
between Literal["foo"] and str. [...]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood Feb 16, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

A TypeVar wouldn't work for this case, sadly, as the relationship here doesn't generalise to other subtypes of str:

>>> class Foo(str): ...
... 
>>> Foo('hi')
'hi'
>>> type(_)
<class '__main__.Foo'>
>>> Foo('hi').capitalize()
'Hi'
>>> type(_)
<class 'str'>

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cdce8p reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@JelleZijlstra JelleZijlstra Feb 16, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think a TypeVar with constraints (similar to AnyStr) might work?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood Feb 16, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hmm — that might work, but might also run into mypy bugs similar to python/typeshed#6762?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@CAM-Gerlach CAM-Gerlach Feb 16, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Don't know if that's the right place for it to ask.

@cdce8p You could also bring it up on the official typing-sig thread for this PEP, which appears to be this one—that might be more generally visible than a comment on a merged PR, if you're looking for a wider discussion.

On a more editorial note, it looks like the PEP hasn't yet been updated with the link to the actual discussion thread, which I only found after a fair amount of determined digging (and guessing). I'll open a PR to add that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cdce8p reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@CAM-Gerlach CAM-Gerlach mentioned this pull request Feb 16, 2022
PEP 675: Add link to official discussion thread #2333
Merged
@erlend-aasland erlend-aasland mentioned this pull request May 13, 2022
pep8/greppable exception messages erlend-aasland/peps#1
Closed
@erlend-aasland erlend-aasland mentioned this pull request Jun 27, 2022
pep 687/mark as accepted erlend-aasland/peps#2
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@AA-Turner AA-Turner AA-Turner left review comments
@CAM-Gerlach CAM-Gerlach CAM-Gerlach approved these changes
@AlexWaygood AlexWaygood AlexWaygood left review comments
+1 more reviewer
@cdce8p cdce8p cdce8p left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@pradeep90 @CAM-Gerlach @JelleZijlstra @AA-Turner @cdce8p @AlexWaygood @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.