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 — typeform: remove a regex call, rearrange checks by JelleZijlstra · Pull Request #21459 · python/mypy

typeform: remove a regex call, rearrange checks #21459

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.

Sign up for GitHub

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

Merged
JelleZijlstra merged 5 commits into python:master from JelleZijlstra:isidentifier
May 10, 2026

Uh oh!

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

Merged

typeform: remove a regex call, rearrange checks #21459

Changes from 4 commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
7468f00
typeform: remove a regex call
JelleZijlstra May 10, 2026
17fa7b9
rearrange a bit
JelleZijlstra May 10, 2026
e75a7a6
catch punctuation
JelleZijlstra May 10, 2026
7e3f058
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 10, 2026
3419c41
len < 2
JelleZijlstra May 10, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

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

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

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

Diff view
Diff view

There are no files selected for viewing

35 changes: 16 additions & 19 deletions 35 mypy/semanal.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,6 @@
# string literal as a type expression.
_MULTIPLE_WORDS_NONTYPE_RE = re.compile(r'\s*[^\s.\'"|\[]+\s+[^\s.\'"|\[]')

# Matches any valid Python identifier, including identifiers with Unicode characters.
#
# [^\d\W] = word character that is not a digit
# \w = word character
# \Z = match end of string; does not allow a trailing \n, unlike $
_IDENTIFIER_RE = re.compile(r"^[^\d\W]\w*\Z", re.UNICODE)


class SemanticAnalyzer(
NodeVisitor[None], SemanticAnalyzerInterface, SemanticAnalyzerPluginInterface, SplittingVisitor
Expand Down Expand Up @@ -8043,16 +8036,9 @@ def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> None:
return
elif isinstance(maybe_type_expr, StrExpr):
str_value = maybe_type_expr.value # cache
# Filter out string literals with common patterns that could not
# possibly be in a type expression
if _MULTIPLE_WORDS_NONTYPE_RE.match(str_value):
# A common pattern in string literals containing a sentence.
# But cannot be a type expression.
maybe_type_expr.as_type = None
return
# Filter out string literals which look like an identifier but
# cannot be a type expression, for a few common reasons
if _IDENTIFIER_RE.fullmatch(str_value):
if str_value.isidentifier():
sym = self.lookup(str_value, UnboundType(str_value), suppress_errors=True)
if sym is None:
# Does not refer to anything in the local symbol table
Expand All @@ -8078,13 +8064,24 @@ def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> None:
return
else: # does not look like an identifier
if '"' in str_value or "'" in str_value:
# Only valid inside a Literal[...] type
# Only valid inside a Literal[...] or Annotated[..., ...] type
if "[" not in str_value:
# Cannot be a Literal[...] type
# Cannot be a Literal[...] or Annotated[..., ...] type
maybe_type_expr.as_type = None
return
elif str_value == "":
# Empty string is not a valid type
elif (
str_value == ""
or str_value.isspace()
or (len(str_value) == 1 and str_value in ".,/:*-=[]\\")
Comment thread Outdated

@hauntsaninja hauntsaninja May 10, 2026

Copy link
Copy Markdown
Collaborator

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.

where does the need for this change come from?

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra May 10, 2026

Copy link
Copy Markdown
Member 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.

According to Codex it accounted for a few % of all strings in mypy.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra May 10, 2026

Copy link
Copy Markdown
Member 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.

The general theory is that it's good to be simple len/containment checks that mypyc optimizes, so we can avoid doing the slower regex call.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Outdated

@ilevkivskyi ilevkivskyi May 10, 2026

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.

Thinking a bit more about this, since we already check for identifier above, are there 1-char strings that are representing a valid Python type? It seems to me we can simply skip all 1-char strings.

Sorry, something went wrong.

Uh oh!

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

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

@JelleZijlstra JelleZijlstra May 10, 2026

Copy link
Copy Markdown
Member 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.

Good point. I think the shortest possible non-identifier valid type expression is actually 3 characters (a.b or a|b), so we can throw out all short strings here.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra May 10, 2026

Copy link
Copy Markdown
Member 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.

Actually two-character strings aren't safe because something like T is valid. But len(str_value) < 2 should be safe.

Sorry, something went wrong.

Uh oh!

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

All reactions
):
# Empty or whitespace-only string is not a valid type
maybe_type_expr.as_type = None
return
# Filter out string literals with common patterns that could not
# possibly be in a type expression
if _MULTIPLE_WORDS_NONTYPE_RE.match(str_value):
# A common pattern in string literals containing a sentence.
# But cannot be a type expression.
maybe_type_expr.as_type = None
return
elif isinstance(maybe_type_expr, IndexExpr):
Expand Down
Loading
Oops, something went wrong.

Uh oh!

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

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 01402792 — 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.