Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-125331: Allow the parser to activate future imports on the fly #125482

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 14, 2025

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Oct 14, 2024

@nineteendo
Copy link
Contributor

nineteendo commented Oct 14, 2024

Eh, not sure if this is desirable, maybe lock the future behind a repl compiler flag:

# tmp.py
from __future__ import barry_as_FLUFL
1 != 2
$ gh-125331/python.exe tmp.py
  File "/Users/wannes/Documents/GitHub/cpython/tmp.py", line 2
    1 != 2
      ^^
SyntaxError: with Barry as BDFL, use '<>' instead of '!='

This is great though:

>>> from __future__ import barry_as_FLUFL
... 1 <> 2
... 
True

@nineteendo
Copy link
Contributor

nineteendo commented Oct 14, 2024

This change isn't backwards compatible, although it only affects 14 files (Github search).

@JelleZijlstra
Copy link
Member

Eh, not sure if this is desirable, maybe lock the future behind a repl compiler flag

I feel it should work like any other future import, so it can serve as an example.

This change isn't backwards compatible, although it only affects 14 files (Github search).

I feel those people are asking for it; they're using an undocumented joke feature. We can limit this change to 3.14 though.

@pablogsal
Copy link
Member Author

pablogsal commented Oct 14, 2024

Eh, not sure if this is desirable, maybe lock the future behind a repl compiler flag:

The whole point of this is that it works everywhere, not just in the REPL

This change isn't backwards compatible, although it only affects 14 files (Github search).

haha, we need to decide if is a bug or if is not. If we decide this should work everywhere the fact that those people are using is fixing the bug. In the issue I said I don't think this is a bug, but if we decide that it is, then it is not backwards incompatible: it's fixing a bug. Notice there is no documentation about how this is supposed to work and all observable behaviour is not protected for backwards compatibility.

@nineteendo
Copy link
Contributor

cc @warsaw

@thatbirdguythatuknownot
Copy link
Contributor

thatbirdguythatuknownot commented Oct 15, 2024

How about the multiple-name imports?

from __future__ import annotations, barry_as_FLUFL
x: A
print(__annotations__['x'])  # A
print(1 <> 2)  # True

If I'm looking at the code correctly, it only accepts single-name imports for now..

Copy link
Member

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

I'm generally okay with this, I think we do some similar hacky things elsewhere.

A couple of minor comments.

@pablogsal
Copy link
Member Author

How about the multiple-name imports?

from __future__ import annotations, barry_as_FLUFL
x: A
print(__annotations__['x'])  # A
print(1 <> 2)  # True

If I'm looking at the code correctly, it only accepts single-name imports for now..

Yeah, the PR is only a draft for evaluating the idea, is still missing many things like tests, NEWs...etc

@pablogsal pablogsal force-pushed the gh-125331 branch 3 times, most recently from cd167c2 to 8fb86b3 Compare October 29, 2024 23:33
@pablogsal
Copy link
Member Author

@lysnikolaou can you take another look?

@pablogsal pablogsal marked this pull request as ready for review October 29, 2024 23:47
Copy link
Member

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉 A couple of very nitty comments. Feel free to diregard.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Thanks Pablo & Jelle for answering my questions!

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Someone alluded to it above, but this incorrectly picks up relative future imports:

>>> from .__future__ import barry_as_FLUFL; 1 <> 2
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    from .__future__ import barry_as_FLUFL; 1 <> 2
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_pyrepl.__future__'
True
>>> from .__future__ import barry_as_FLUFLx; 1 <> 2
  File "<python-input-5>", line 1
    from .__future__ import barry_as_FLUFLx; 1 <> 2
                                               ^^
SyntaxError: invalid syntax

The first one correctly throws ModuleNotFoundError, but it also allows 1 <> 2. The second one throws a SyntaxError as expected.

@pablogsal
Copy link
Member Author

pablogsal commented Feb 13, 2025

@JelleZijlstra I have fixed the relative imports, rebased and added a test, can you review it again?

@pablogsal pablogsal enabled auto-merge (squash) February 13, 2025 00:51
@pablogsal pablogsal merged commit 3bd3e09 into python:main Feb 14, 2025
45 of 46 checks passed
@pablogsal pablogsal deleted the gh-125331 branch February 14, 2025 04:54
@pablogsal pablogsal added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Mar 10, 2025
@miss-islington-app
Copy link

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

@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 10, 2025
…ly (pythonGH-125482)

(cherry picked from commit 3bd3e09)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Mar 10, 2025

GH-131062 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Mar 10, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 10, 2025
…ly (pythonGH-125482)

(cherry picked from commit 3bd3e09)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Mar 10, 2025

GH-131063 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Mar 10, 2025
pablogsal added a commit that referenced this pull request Mar 10, 2025
…fly (GH-125482) (#131063)

gh-125331: Allow the parser to activate future imports on the fly (GH-125482)
(cherry picked from commit 3bd3e09)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
pablogsal added a commit that referenced this pull request Mar 10, 2025
…fly (GH-125482) (#131062)

gh-125331: Allow the parser to activate future imports on the fly (GH-125482)
(cherry picked from commit 3bd3e09)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants