Skip to content

gh-135676: lexical analysis: Improve section on Numeric literals - #134850

Merged
encukou merged 7 commits into
python:mainfrom
encukou:lex-analysis-numbers
Jun 18, 2025
Merged

gh-135676: lexical analysis: Improve section on Numeric literals#134850
encukou merged 7 commits into
python:mainfrom
encukou:lex-analysis-numbers

Conversation

@encukou

@encukouencukou commented May 28, 2025

Copy link
Copy Markdown
Member

Each section is expanded, and now progresses from simple cases and examples to more involved ones, ending with the formal grammar.

This uses meta-syntax that I want to add to the introduction in #134443. I'm marking it as draft until that's merged.


📚 Documentation preview 📚: https://cpython-previews--134850.org.readthedocs.build/

Comment threadDoc/reference/lexical_analysis.rst Outdated
Comment on lines +1090 to +1094
Instead, :ref:`complex numbers <typesnumeric>` can be written
as adding the complex number's real part and imaginary part.

.. productionlist:: python-grammar
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
For example, the imaginary number 3.1+4\ *i* can be written as adding the
real number 3.1 to the imaginary number 4\ *i*.

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.

Sorry, but this is a wrong picture.

The CPython has no pure-imaginary numbers and above decomposition is invalid in general. Simple counterexample:

>>> -0.0+0j
0j
>>> complex(-0.0, 0)
(-0+0j)

Previous description was valid: "An imaginary literal yields a complex number with a real part of 0.0."

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.

The key point is this is lexical analysis. Python indeed has no imaginary literals, but the tokeniser does -- 4+16j is three tokens.

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.

I think L1093 should say 'the complex number ...', though.

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.

Sure, but 16j is not an imaginary number. It's a complex number with a real part +0.0. And complex number in the Python currently can't be "written as adding the complex number's real part and imaginary part."

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

16j is not an imaginary number; but 16i is :)

I agree that this could be clearer. Will update. (Not sure when, as I recently volunteered to focus on f-strings first.)

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.

16j is not an imaginary number; but 16i is :)

But you are talking not about mathematics, but Python's complex numbers.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I've reworded the intro for Imaginary literals. Is this clearer?

@encukou
encukou marked this pull request as ready for review June 11, 2025 14:17
@skirpichev
skirpichev self-requested a review June 11, 2025 14:50

@skirpichevskirpichev 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.

This pr mix

  1. change productlist directive to grammar-snippet, that solves referenced issue
  2. substantial rewrite of sections about literals.

I don't see which issue solves 2). But, maybe it's better to do in a separate pr?

@encukou

Copy link
Copy Markdown
MemberAuthor

This PR is about the rewrite; changing the directives is a minor part of that.

Perhaps I could use more granular issues -- have one for adding the grammar-snippet directive itself, and another for rewriting Lexical analysis, and a different one for the Language Reference. I'm not sure if that would make things clearer, though.

@encukouencukou changed the title gh-127833: lexical analysis: Improve section on Numeric literalsgh-135676: lexical analysis: Improve section on Numeric literalsJun 18, 2025
@encukou

Copy link
Copy Markdown
MemberAuthor

I've opened #135676 for rewording Lexical analysis, and attached this PR to that.

@ryan-duveryan-duve left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed online with @encukou.

@encukou

Copy link
Copy Markdown
MemberAuthor

Merging; please note any follow-ups on the issue :)

@encukou
encukou merged commit 21f3d15 into python:mainJun 18, 2025
@github-project-automationgithub-project-automationBot moved this from Todo to Done in Docs PRsJun 18, 2025
@encukou
encukou deleted the lex-analysis-numbers branch June 18, 2025 14:34
@encukouencukou added the needs backport to 3.14 bugs and security fixes label Jun 18, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 18, 2025
pythonGH-134850)
(cherry picked from commit 21f3d15)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@bedevere-app

Copy link
Copy Markdown

GH-135677 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Jun 18, 2025
encukou added a commit that referenced this pull request Jun 18, 2025
…ls (GH-134850) (GH-135677)
gh-135676: lexical analysis: Improve section on Numeric literals (GH-134850)
(cherry picked from commit 21f3d15)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Comment threadDoc/reference/lexical_analysis.rst
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants

@encukou@skirpichev@ryan-duve@AA-Turner@StanFromIreland