all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [RFE] python-mode: removing `file` keyword from syntax highlight
@ 2025-01-22 12:57 Konstantin Kharlamov
  2025-01-24  7:50 ` Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-22 12:57 UTC (permalink / raw)
  To: emacs-devel

Seems to be a simple change, but knowing sometimes such things raise
discussions, decided to ask here first.

While working with Python I frequently notice word `file` highlighted
as a keyword, even though I never remember using such library function
or a keyword.

Some digging showed that `file` was a built-in type in Python2 that's
gone in Python3 in preference of `open()` function which returns type
`IO[Any]`.

Nowadays word `file` is just a typical variable name you can see while
working with open (pun intended), case in point¹. Seeing it's
highlighted as if it's any special is annoying. Given that Python2 has
been dead for ½ a decade at this point, can we perhaps remove `file`
from the list of keywords?

1: https://stackoverflow.com/a/8369345/2388257



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-22 12:57 [RFE] python-mode: removing `file` keyword from syntax highlight Konstantin Kharlamov
@ 2025-01-24  7:50 ` Konstantin Kharlamov
  2025-01-24 16:03   ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-24  7:50 UTC (permalink / raw)
  To: emacs-devel; +Cc: kobarity

On Wed, 2025-01-22 at 15:57 +0300, Konstantin Kharlamov wrote:
> Seems to be a simple change, but knowing sometimes such things raise
> discussions, decided to ask here first.
> 
> While working with Python I frequently notice word `file` highlighted
> as a keyword, even though I never remember using such library
> function
> or a keyword.
> 
> Some digging showed that `file` was a built-in type in Python2 that's
> gone in Python3 in preference of `open()` function which returns type
> `IO[Any]`.
> 
> Nowadays word `file` is just a typical variable name you can see
> while
> working with open (pun intended), case in point¹. Seeing it's
> highlighted as if it's any special is annoying. Given that Python2
> has
> been dead for ½ a decade at this point, can we perhaps remove `file`
> from the list of keywords?
> 
> 1: https://stackoverflow.com/a/8369345/2388257

CC: kobarity



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24  7:50 ` Konstantin Kharlamov
@ 2025-01-24 16:03   ` kobarity
  2025-01-24 17:08     ` Stefan Kangas
  2025-01-24 17:28     ` Konstantin Kharlamov
  0 siblings, 2 replies; 35+ messages in thread
From: kobarity @ 2025-01-24 16:03 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: emacs-devel

Konstantin Kharlamov wrote:
> 
> On Wed, 2025-01-22 at 15:57 +0300, Konstantin Kharlamov wrote:
> > Seems to be a simple change, but knowing sometimes such things raise
> > discussions, decided to ask here first.
> > 
> > While working with Python I frequently notice word `file` highlighted
> > as a keyword, even though I never remember using such library
> > function
> > or a keyword.
> > 
> > Some digging showed that `file` was a built-in type in Python2 that's
> > gone in Python3 in preference of `open()` function which returns type
> > `IO[Any]`.
> > 
> > Nowadays word `file` is just a typical variable name you can see
> > while
> > working with open (pun intended), case in point¹. Seeing it's
> > highlighted as if it's any special is annoying. Given that Python2
> > has
> > been dead for ½ a decade at this point, can we perhaps remove `file`
> > from the list of keywords?
> > 
> > 1: https://stackoverflow.com/a/8369345/2388257
> 
> CC: kobarity

python-mode highlights both Python 2 and Python 3 keywords:

           ;; Python 2:
           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
           "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
           "intern"
           ;; Python 3:
           "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"

I think this is a compromise based on the idea that it is better to
have a few extra words highlighted than to have keywords that are not
highlighted.  As I wrote in #70815, I personally do not want to drop
supporting Python 2 yet.

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=70815

I am also one of those in a position where I still need to support
Python 2 code.

However, I know that Python 2 has already been EOL for a long time and
that very few people still use it.  Furthermore, even if some
keywords are not highlighted, it does not mean that we cannot edit
Python 2 code.  So I am not strongly against removing the Python 2
keywords if many people want to.

One workaround to prevent Python 2 keywords from being highlighted
would be to use python-ts-mode.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24 16:03   ` kobarity
@ 2025-01-24 17:08     ` Stefan Kangas
  2025-01-24 17:28     ` Konstantin Kharlamov
  1 sibling, 0 replies; 35+ messages in thread
From: Stefan Kangas @ 2025-01-24 17:08 UTC (permalink / raw)
  To: kobarity, Konstantin Kharlamov; +Cc: emacs-devel

kobarity <kobarity@gmail.com> writes:

> However, I know that Python 2 has already been EOL for a long time and
> that very few people still use it.  Furthermore, even if some
> keywords are not highlighted, it does not mean that we cannot edit
> Python 2 code.  So I am not strongly against removing the Python 2
> keywords if many people want to.

Yeah, I think that's a good way to look at it.  It's clear that it will
happen at some point, but I don't have a strong opinion on whether or
not it should happen in Emacs 31 or not.

6% of developers responding to the Python Developer's Survey 2023 were
still using Python 2:

https://lp.jetbrains.com/python-developers-survey-2023/#python-versions

But that means that 94% are using Python 3.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24 16:03   ` kobarity
  2025-01-24 17:08     ` Stefan Kangas
@ 2025-01-24 17:28     ` Konstantin Kharlamov
  2025-01-24 19:14       ` Howard Melman
  2025-01-25 11:10       ` Kévin Le Gouguec
  1 sibling, 2 replies; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-24 17:28 UTC (permalink / raw)
  To: kobarity; +Cc: emacs-devel

On Sat, 2025-01-25 at 01:03 +0900, kobarity wrote:
> Konstantin Kharlamov wrote:
> > 
> > On Wed, 2025-01-22 at 15:57 +0300, Konstantin Kharlamov wrote:
> > > Seems to be a simple change, but knowing sometimes such things
> > > raise
> > > discussions, decided to ask here first.
> > > 
> > > While working with Python I frequently notice word `file`
> > > highlighted
> > > as a keyword, even though I never remember using such library
> > > function
> > > or a keyword.
> > > 
> > > Some digging showed that `file` was a built-in type in Python2
> > > that's
> > > gone in Python3 in preference of `open()` function which returns
> > > type
> > > `IO[Any]`.
> > > 
> > > Nowadays word `file` is just a typical variable name you can see
> > > while
> > > working with open (pun intended), case in point¹. Seeing it's
> > > highlighted as if it's any special is annoying. Given that
> > > Python2
> > > has
> > > been dead for ½ a decade at this point, can we perhaps remove
> > > `file`
> > > from the list of keywords?
> > > 
> > > 1: https://stackoverflow.com/a/8369345/2388257
> > 
> > CC: kobarity
> 
> python-mode highlights both Python 2 and Python 3 keywords:
> 
>            ;; Python 2:
>            "basestring" "cmp" "execfile" "file" "long" "raw_input"
> "reduce"
>            "reload" "unichr" "unicode" "xrange" "apply" "buffer"
> "coerce"
>            "intern"
>            ;; Python 3:
>            "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes"
> "exec"
> 
> I think this is a compromise based on the idea that it is better to
> have a few extra words highlighted than to have keywords that are not
> highlighted.  As I wrote in #70815, I personally do not want to drop
> supporting Python 2 yet.
> 
> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=70815
> 
> I am also one of those in a position where I still need to support
> Python 2 code.
> 
> However, I know that Python 2 has already been EOL for a long time
> and
> that very few people still use it.  Furthermore, even if some
> keywords are not highlighted, it does not mean that we cannot edit
> Python 2 code.  So I am not strongly against removing the Python 2
> keywords if many people want to.

I see. Okay, what do you think then about adding to the mode
initialization a check that would check if the first line of the buffer
has word `python2`, and would otherwise assume it is python3 and turn
off the python2 keywords highlight?



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24 17:28     ` Konstantin Kharlamov
@ 2025-01-24 19:14       ` Howard Melman
  2025-01-25  8:25         ` Konstantin Kharlamov
  2025-01-25 11:10       ` Kévin Le Gouguec
  1 sibling, 1 reply; 35+ messages in thread
From: Howard Melman @ 2025-01-24 19:14 UTC (permalink / raw)
  To: emacs-devel

Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:

>> I am also one of those in a position where I still need to support
>> Python 2 code.
>> 
>> However, I know that Python 2 has already been EOL for a long time
>> and that very few people still use it.  Furthermore, even if some
>> keywords are not highlighted, it does not mean that we cannot edit
>> Python 2 code.  So I am not strongly against removing the Python 2
>> keywords if many people want to.
>
> I see. Okay, what do you think then about adding to the mode
> initialization a check that would check if the first line of the buffer
> has word `python2`, and would otherwise assume it is python3 and turn
> off the python2 keywords highlight?

Why not have a python2-mode that derives from python-mode
with a few changes in it?

-- 

Howard




^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24 19:14       ` Howard Melman
@ 2025-01-25  8:25         ` Konstantin Kharlamov
  0 siblings, 0 replies; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-25  8:25 UTC (permalink / raw)
  To: Howard Melman, emacs-devel

On Fri, 2025-01-24 at 14:14 -0500, Howard Melman wrote:
> Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> 
> > > I am also one of those in a position where I still need to
> > > support
> > > Python 2 code.
> > > 
> > > However, I know that Python 2 has already been EOL for a long
> > > time
> > > and that very few people still use it.  Furthermore, even if some
> > > keywords are not highlighted, it does not mean that we cannot
> > > edit
> > > Python 2 code.  So I am not strongly against removing the Python
> > > 2
> > > keywords if many people want to.
> > 
> > I see. Okay, what do you think then about adding to the mode
> > initialization a check that would check if the first line of the
> > buffer
> > has word `python2`, and would otherwise assume it is python3 and
> > turn
> > off the python2 keywords highlight?
> 
> Why not have a python2-mode that derives from python-mode
> with a few changes in it?

I didn't look at it, but I would presume there may be places where
python3 and python2 keywords are mixed, so just adding an
initialization switch may be simpler. We already have an example of
such mode: diff-mode has detection of whether current diff is produced
by `git` to cover for `--` symbol being a separator rather than a
"deleted line".



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-24 17:28     ` Konstantin Kharlamov
  2025-01-24 19:14       ` Howard Melman
@ 2025-01-25 11:10       ` Kévin Le Gouguec
  2025-01-25 11:23         ` Ship Mints
  1 sibling, 1 reply; 35+ messages in thread
From: Kévin Le Gouguec @ 2025-01-25 11:10 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: kobarity, emacs-devel

Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:

>> However, I know that Python 2 has already been EOL for a long time and
>> that very few people still use it.  Furthermore, even if some
>> keywords are not highlighted, it does not mean that we cannot edit
>> Python 2 code.  So I am not strongly against removing the Python 2
>> keywords if many people want to.
>
> I see. Okay, what do you think then about adding to the mode
> initialization a check that would check if the first line of the buffer
> has word `python2`, and would otherwise assume it is python3 and turn
> off the python2 keywords highlight?

An idea that I've never acted on:

* a defcustom (python-dialect?) to pick between 2, 3, 'guess,

* python-mode initialization would amend font-lock-defaults depending on
  that option (not unlike e.g. sh-builtins which maps shells to literals
  to sh-font-lock-keywords, which eventually finds its way to
  font-lock-defaults)

* 'guess could, as you say, peek at the shebang to hopefully do TRT,

* (maybe an additional value ('six?) to ask for "maximum coverage", for
  codebases that purport to still support 2 & 3?)

* users could explicitly set that variable per-project via
  .dir-locals.el.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-25 11:10       ` Kévin Le Gouguec
@ 2025-01-25 11:23         ` Ship Mints
  2025-01-25 15:08           ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Ship Mints @ 2025-01-25 11:23 UTC (permalink / raw)
  To: Kévin Le Gouguec; +Cc: Konstantin Kharlamov, kobarity, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1698 bytes --]

To me, it's not worth the trouble to have two modes, or extra configuration
settings, or complex dynamic detectors just to deprecate a couple of now
non-keywords.

I'd rather have "file" no longer be a keyword than incorrectly display it
as a keyword in now more popular python3 code.

-Stephane

On Sat, Jan 25, 2025 at 6:11 AM Kévin Le Gouguec <kevin.legouguec@gmail.com>
wrote:

> Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
>
> >> However, I know that Python 2 has already been EOL for a long time and
> >> that very few people still use it.  Furthermore, even if some
> >> keywords are not highlighted, it does not mean that we cannot edit
> >> Python 2 code.  So I am not strongly against removing the Python 2
> >> keywords if many people want to.
> >
> > I see. Okay, what do you think then about adding to the mode
> > initialization a check that would check if the first line of the buffer
> > has word `python2`, and would otherwise assume it is python3 and turn
> > off the python2 keywords highlight?
>
> An idea that I've never acted on:
>
> * a defcustom (python-dialect?) to pick between 2, 3, 'guess,
>
> * python-mode initialization would amend font-lock-defaults depending on
>   that option (not unlike e.g. sh-builtins which maps shells to literals
>   to sh-font-lock-keywords, which eventually finds its way to
>   font-lock-defaults)
>
> * 'guess could, as you say, peek at the shebang to hopefully do TRT,
>
> * (maybe an additional value ('six?) to ask for "maximum coverage", for
>   codebases that purport to still support 2 & 3?)
>
> * users could explicitly set that variable per-project via
>   .dir-locals.el.
>
>

[-- Attachment #2: Type: text/html, Size: 2572 bytes --]

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-25 11:23         ` Ship Mints
@ 2025-01-25 15:08           ` kobarity
  2025-01-25 20:54             ` Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-01-25 15:08 UTC (permalink / raw)
  To: Ship Mints, Kévin Le Gouguec, Konstantin Kharlamov; +Cc: emacs-devel

Kévin Le Gouguec wrote:
> Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> 
> >> However, I know that Python 2 has already been EOL for a long time and
> >> that very few people still use it.  Furthermore, even if some
> >> keywords are not highlighted, it does not mean that we cannot edit
> >> Python 2 code.  So I am not strongly against removing the Python 2
> >> keywords if many people want to.
> >
> > I see. Okay, what do you think then about adding to the mode
> > initialization a check that would check if the first line of the buffer
> > has word `python2`, and would otherwise assume it is python3 and turn
> > off the python2 keywords highlight?
> 
> An idea that I've never acted on:
> 
> * a defcustom (python-dialect?) to pick between 2, 3, 'guess,
> 
> * python-mode initialization would amend font-lock-defaults depending on
>   that option (not unlike e.g. sh-builtins which maps shells to literals
>   to sh-font-lock-keywords, which eventually finds its way to
>   font-lock-defaults)
> 
> * 'guess could, as you say, peek at the shebang to hopefully do TRT,
> 
> * (maybe an additional value ('six?) to ask for "maximum coverage", for
>   codebases that purport to still support 2 & 3?)
> 
> * users could explicitly set that variable per-project via
>   .dir-locals.el.

I don't think "guessing" will work, since the shebang is not always
available.

Ship Mints wrote:
> To me, it's not worth the trouble to have two modes, or extra configuration settings, or complex dynamic detectors just
> to deprecate a couple of now non-keywords.
> 
> I'd rather have "file" no longer be a keyword than incorrectly display it as a keyword in now more popular python3 code.
> 
> -Stephane

I think `defcustom' is one option, but as it requires to update some
variables such as `python-font-lock-keywords-level-2',
`python-font-lock-keywords-maximum-decoration', and
`python-font-lock-keywords', I agree that it's not worth introducing
now.

Since no one else seems to want it to remain, it would be appropriate
to remove it.

Are you going to remove only "file"?  Or are you going to delete all
the Python 2 keywords?



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-25 15:08           ` kobarity
@ 2025-01-25 20:54             ` Konstantin Kharlamov
  2025-01-27 14:27               ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-25 20:54 UTC (permalink / raw)
  To: kobarity, Ship Mints, Kévin Le Gouguec; +Cc: emacs-devel

On Sun, 2025-01-26 at 00:08 +0900, kobarity wrote:
> Kévin Le Gouguec wrote:
> > Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> > 
> > > > However, I know that Python 2 has already been EOL for a long
> > > > time and
> > > > that very few people still use it.  Furthermore, even if some
> > > > keywords are not highlighted, it does not mean that we cannot
> > > > edit
> > > > Python 2 code.  So I am not strongly against removing the
> > > > Python 2
> > > > keywords if many people want to.
> > > 
> > > I see. Okay, what do you think then about adding to the mode
> > > initialization a check that would check if the first line of the
> > > buffer
> > > has word `python2`, and would otherwise assume it is python3 and
> > > turn
> > > off the python2 keywords highlight?
> > 
> > An idea that I've never acted on:
> > 
> > * a defcustom (python-dialect?) to pick between 2, 3, 'guess,
> > 
> > * python-mode initialization would amend font-lock-defaults
> > depending on
> >   that option (not unlike e.g. sh-builtins which maps shells to
> > literals
> >   to sh-font-lock-keywords, which eventually finds its way to
> >   font-lock-defaults)
> > 
> > * 'guess could, as you say, peek at the shebang to hopefully do
> > TRT,
> > 
> > * (maybe an additional value ('six?) to ask for "maximum coverage",
> > for
> >   codebases that purport to still support 2 & 3?)
> > 
> > * users could explicitly set that variable per-project via
> >   .dir-locals.el.
> 
> I don't think "guessing" will work, since the shebang is not always
> available.
> 
> Ship Mints wrote:
> > To me, it's not worth the trouble to have two modes, or extra
> > configuration settings, or complex dynamic detectors just
> > to deprecate a couple of now non-keywords.
> > 
> > I'd rather have "file" no longer be a keyword than incorrectly
> > display it as a keyword in now more popular python3 code.
> > 
> > -Stephane
> 
> I think `defcustom' is one option, but as it requires to update some
> variables such as `python-font-lock-keywords-level-2',
> `python-font-lock-keywords-maximum-decoration', and
> `python-font-lock-keywords', I agree that it's not worth introducing
> now.
> 
> Since no one else seems to want it to remain, it would be appropriate
> to remove it.
> 
> Are you going to remove only "file"?  Or are you going to delete all
> the Python 2 keywords?

If that's a question for me: I have no grudge against other keywords.
Looking at the list in the source code¹, I think the only other keyword
one can see as an identifier is `cmp` (it would be code where you chose
a comprision-function from a list, and it's fair to name the variable
just `cmp`). Other words (like `long`) you'd typically see as part of a
larger word; `buffer` I don't remember for anyone to write in full,
people are more likely to type just `buf`. Then there's `reduce`, which
is Python's `foldl` function, but given that `all` and `any` are
highlighted as keywords, that would be strange to remove `reduce`…

So ultimately, I personally don't have problems with other keywords
besides `file`. It's just that `file` really comes up a lot in a code.

1:
https://github.com/emacs-mirror/emacs/blob/35d39278599caf30eb4bfbd83118ffe15d2bc705/lisp/progmodes/python.el#L717-L720



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [RFE] python-mode: removing `file` keyword from syntax highlight
  2025-01-25 20:54             ` Konstantin Kharlamov
@ 2025-01-27 14:27               ` kobarity
  2025-01-29  5:48                 ` [PATCH] Remove Python2 "file" from the list of keywords Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-01-27 14:27 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

Konstantin Kharlamov wrote:
> 
> On Sun, 2025-01-26 at 00:08 +0900, kobarity wrote:
> > Kévin Le Gouguec wrote:
> > > Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> > > 
> > > > > However, I know that Python 2 has already been EOL for a long
> > > > > time and
> > > > > that very few people still use it.  Furthermore, even if some
> > > > > keywords are not highlighted, it does not mean that we cannot
> > > > > edit
> > > > > Python 2 code.  So I am not strongly against removing the
> > > > > Python 2
> > > > > keywords if many people want to.
> > > > 
> > > > I see. Okay, what do you think then about adding to the mode
> > > > initialization a check that would check if the first line of the
> > > > buffer
> > > > has word `python2`, and would otherwise assume it is python3 and
> > > > turn
> > > > off the python2 keywords highlight?
> > > 
> > > An idea that I've never acted on:
> > > 
> > > * a defcustom (python-dialect?) to pick between 2, 3, 'guess,
> > > 
> > > * python-mode initialization would amend font-lock-defaults
> > > depending on
> > >   that option (not unlike e.g. sh-builtins which maps shells to
> > > literals
> > >   to sh-font-lock-keywords, which eventually finds its way to
> > >   font-lock-defaults)
> > > 
> > > * 'guess could, as you say, peek at the shebang to hopefully do
> > > TRT,
> > > 
> > > * (maybe an additional value ('six?) to ask for "maximum coverage",
> > > for
> > >   codebases that purport to still support 2 & 3?)
> > > 
> > > * users could explicitly set that variable per-project via
> > >   .dir-locals.el.
> > 
> > I don't think "guessing" will work, since the shebang is not always
> > available.
> > 
> > Ship Mints wrote:
> > > To me, it's not worth the trouble to have two modes, or extra
> > > configuration settings, or complex dynamic detectors just
> > > to deprecate a couple of now non-keywords.
> > > 
> > > I'd rather have "file" no longer be a keyword than incorrectly
> > > display it as a keyword in now more popular python3 code.
> > > 
> > > -Stephane
> > 
> > I think `defcustom' is one option, but as it requires to update some
> > variables such as `python-font-lock-keywords-level-2',
> > `python-font-lock-keywords-maximum-decoration', and
> > `python-font-lock-keywords', I agree that it's not worth introducing
> > now.
> > 
> > Since no one else seems to want it to remain, it would be appropriate
> > to remove it.
> > 
> > Are you going to remove only "file"?  Or are you going to delete all
> > the Python 2 keywords?
> 
> If that's a question for me: I have no grudge against other keywords.
> Looking at the list in the source code¹, I think the only other keyword
> one can see as an identifier is `cmp` (it would be code where you chose
> a comprision-function from a list, and it's fair to name the variable
> just `cmp`). Other words (like `long`) you'd typically see as part of a
> larger word; `buffer` I don't remember for anyone to write in full,
> people are more likely to type just `buf`. Then there's `reduce`, which
> is Python's `foldl` function, but given that `all` and `any` are
> highlighted as keywords, that would be strange to remove `reduce`…
> 
> So ultimately, I personally don't have problems with other keywords
> besides `file`. It's just that `file` really comes up a lot in a code.
> 
> 1:
> https://github.com/emacs-mirror/emacs/blob/35d39278599caf30eb4bfbd83118ffe15d2bc705/lisp/progmodes/python.el#L717-L720

Thanks, understood.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH] Remove Python2 "file" from the list of keywords
  2025-01-27 14:27               ` kobarity
@ 2025-01-29  5:48                 ` Konstantin Kharlamov
  2025-01-29 14:26                   ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-29  5:48 UTC (permalink / raw)
  To: kobarity; +Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 99 bytes --]

> 
So, it's been a few days, nobody seems against, so I'm attaching a
patch. Please review 😊

[-- Attachment #2: 1.patch --]
[-- Type: text/x-patch, Size: 1553 bytes --]

From 997ac0d374d3085d822f2a3d4aa985016bcf0d44 Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Date: Wed, 29 Jan 2025 08:35:31 +0300
Subject: [PATCH] Remove Python2 "file" from the list of keywords

`file` was a built-in type in Python2, removed in Python3.  Nowadays,
word `file` is a frequent identifier name that people use in short
file-related code sections.  Having it highlighted looks odd in a
code.  As a prior art, Vim and VS Code don't currently highlight
`file` specially.

This was recently discussed on emacs-devel with the conclusion it is
okay to remove `file` from the list of keywords.

* lisp/progmodes/python.el (python-font-lock-keywords-level-2): Remove
"file" from the list.
---
 lisp/progmodes/python.el | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index c00de2d6a8d..63798687928 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
            "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
            "__import__"
            ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
+           "basestring" "cmp" "execfile" "intern" "long" "raw_input" "reduce"
            "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
            ;; Python 3:
            "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
            ;; Special attributes:
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH] Remove Python2 "file" from the list of keywords
  2025-01-29  5:48                 ` [PATCH] Remove Python2 "file" from the list of keywords Konstantin Kharlamov
@ 2025-01-29 14:26                   ` kobarity
  2025-01-29 22:19                     ` [PATCH v2] " Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-01-29 14:26 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

Konstantin Kharlamov wrote:
> So, it's been a few days, nobody seems against, so I'm attaching a
> patch. Please review 😊

I think it should be announced in etc/NEWS.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2] Remove Python2 "file" from the list of keywords
  2025-01-29 14:26                   ` kobarity
@ 2025-01-29 22:19                     ` Konstantin Kharlamov
  2025-01-30  1:56                       ` Stefan Kangas
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-29 22:19 UTC (permalink / raw)
  To: kobarity; +Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 257 bytes --]

On Wed, 2025-01-29 at 23:26 +0900, kobarity wrote:
> Konstantin Kharlamov wrote:
> > So, it's been a few days, nobody seems against, so I'm attaching a
> > patch. Please review 😊
> 
> I think it should be announced in etc/NEWS.

Thank you, done!

[-- Attachment #2: 1.patch --]
[-- Type: text/x-patch, Size: 2218 bytes --]

From ffda4e954d157b544453b388458f1ec414d17abd Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Date: Wed, 29 Jan 2025 08:35:31 +0300
Subject: [PATCH v2] Remove Python2 "file" from the list of keywords

`file` was a built-in type in Python2, removed in Python3.  Nowadays,
word `file` is a frequent identifier name that people use in short
file-related code sections.  Having it highlighted looks odd in a
code.  As a prior art, Vim and VS Code don't currently highlight
`file` specially.

This was recently discussed on emacs-devel with the conclusion it is
okay to remove `file` from the list of keywords.

* etc/NEWS: Announce "file" keyword removal.
* lisp/progmodes/python.el (python-font-lock-keywords-level-2): Remove
"file" from the list.
---
 etc/NEWS                 | 6 ++++++
 lisp/progmodes/python.el | 3 +--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index f7e9f283709..ca16079ad64 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -807,6 +807,12 @@ exist.  If "python" points to Python 2 on your system, you now have to
 customize these variables to "python3" if you want to use Python 3
 instead.
 
+---
+*** Remove "file" from the list of keywords.
+In Python 3 "file" has no special meaning, and in modern code the word
+is frequently used as a variable name.  To make sure highlight in such
+code doesn't look off, we no longer highlight "file".
+
 ---
 *** Support of 'electric-layout-mode' added.
 
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index c00de2d6a8d..63798687928 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
            "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
            "__import__"
            ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
+           "basestring" "cmp" "execfile" "intern" "long" "raw_input" "reduce"
            "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
            ;; Python 3:
            "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
            ;; Special attributes:
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v2] Remove Python2 "file" from the list of keywords
  2025-01-29 22:19                     ` [PATCH v2] " Konstantin Kharlamov
@ 2025-01-30  1:56                       ` Stefan Kangas
  2025-01-30  7:07                         ` Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: Stefan Kangas @ 2025-01-30  1:56 UTC (permalink / raw)
  To: Konstantin Kharlamov, kobarity
  Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:

> On Wed, 2025-01-29 at 23:26 +0900, kobarity wrote:
>> Konstantin Kharlamov wrote:
>> > So, it's been a few days, nobody seems against, so I'm attaching a
>> > patch. Please review 😊
>>
>> I think it should be announced in etc/NEWS.
>
> Thank you, done!
> From ffda4e954d157b544453b388458f1ec414d17abd Mon Sep 17 00:00:00 2001
> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Date: Wed, 29 Jan 2025 08:35:31 +0300
> Subject: [PATCH v2] Remove Python2 "file" from the list of keywords
>
> `file` was a built-in type in Python2, removed in Python3.  Nowadays,
> word `file` is a frequent identifier name that people use in short
> file-related code sections.  Having it highlighted looks odd in a
> code.  As a prior art, Vim and VS Code don't currently highlight
> `file` specially.
>
> This was recently discussed on emacs-devel with the conclusion it is
> okay to remove `file` from the list of keywords.
>
> * etc/NEWS: Announce "file" keyword removal.
> * lisp/progmodes/python.el (python-font-lock-keywords-level-2): Remove
> "file" from the list.
> ---
>  etc/NEWS                 | 6 ++++++
>  lisp/progmodes/python.el | 3 +--
>  2 files changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/etc/NEWS b/etc/NEWS
> index f7e9f283709..ca16079ad64 100644
> --- a/etc/NEWS
> +++ b/etc/NEWS
> @@ -807,6 +807,12 @@ exist.  If "python" points to Python 2 on your system, you now have to
>  customize these variables to "python3" if you want to use Python 3
>  instead.
>
> +---
> +*** Remove "file" from the list of keywords.
> +In Python 3 "file" has no special meaning, and in modern code the word
> +is frequently used as a variable name.  To make sure highlight in such
> +code doesn't look off, we no longer highlight "file".
> +
>  ---
>  *** Support of 'electric-layout-mode' added.
>
> diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
> index c00de2d6a8d..63798687928 100644
> --- a/lisp/progmodes/python.el
> +++ b/lisp/progmodes/python.el
> @@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
>             "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
>             "__import__"
>             ;; Python 2:
> -           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
> +           "basestring" "cmp" "execfile" "intern" "long" "raw_input" "reduce"
>             "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
> -           "intern"

I understand that "file" probably the top most common one, but, looking
at the above list of builtins, many of the remaining ones should be
attractive names to use as well:

    apply
    basestring
    buffer
    cmp
    coerce
    execfile
    file
    intern
    long
    raw_input
    reduce
    reload
    unichr
    unicode
    xrange

If we're anyways making changes here, maybe we should just take the
plunge and drop all of the Python 2 builtins?  It should be relatively
harmless for Python 2 users, I think, and it's easier to explain what
we're doing in etc/NEWS.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2] Remove Python2 "file" from the list of keywords
  2025-01-30  1:56                       ` Stefan Kangas
@ 2025-01-30  7:07                         ` Konstantin Kharlamov
  2025-01-30 15:15                           ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-30  7:07 UTC (permalink / raw)
  To: Stefan Kangas, kobarity; +Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

On Wed, 2025-01-29 at 19:56 -0600, Stefan Kangas wrote:
> Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> 
> > On Wed, 2025-01-29 at 23:26 +0900, kobarity wrote:
> > > Konstantin Kharlamov wrote:
> > > > So, it's been a few days, nobody seems against, so I'm
> > > > attaching a
> > > > patch. Please review 😊
> > > 
> > > I think it should be announced in etc/NEWS.
> > 
> > Thank you, done!
> > From ffda4e954d157b544453b388458f1ec414d17abd Mon Sep 17 00:00:00
> > 2001
> > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > Date: Wed, 29 Jan 2025 08:35:31 +0300
> > Subject: [PATCH v2] Remove Python2 "file" from the list of keywords
> > 
> > `file` was a built-in type in Python2, removed in Python3. 
> > Nowadays,
> > word `file` is a frequent identifier name that people use in short
> > file-related code sections.  Having it highlighted looks odd in a
> > code.  As a prior art, Vim and VS Code don't currently highlight
> > `file` specially.
> > 
> > This was recently discussed on emacs-devel with the conclusion it
> > is
> > okay to remove `file` from the list of keywords.
> > 
> > * etc/NEWS: Announce "file" keyword removal.
> > * lisp/progmodes/python.el (python-font-lock-keywords-level-2):
> > Remove
> > "file" from the list.
> > ---
> >  etc/NEWS                 | 6 ++++++
> >  lisp/progmodes/python.el | 3 +--
> >  2 files changed, 7 insertions(+), 2 deletions(-)
> > 
> > diff --git a/etc/NEWS b/etc/NEWS
> > index f7e9f283709..ca16079ad64 100644
> > --- a/etc/NEWS
> > +++ b/etc/NEWS
> > @@ -807,6 +807,12 @@ exist.  If "python" points to Python 2 on your
> > system, you now have to
> >  customize these variables to "python3" if you want to use Python 3
> >  instead.
> > 
> > +---
> > +*** Remove "file" from the list of keywords.
> > +In Python 3 "file" has no special meaning, and in modern code the
> > word
> > +is frequently used as a variable name.  To make sure highlight in
> > such
> > +code doesn't look off, we no longer highlight "file".
> > +
> >  ---
> >  *** Support of 'electric-layout-mode' added.
> > 
> > diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
> > index c00de2d6a8d..63798687928 100644
> > --- a/lisp/progmodes/python.el
> > +++ b/lisp/progmodes/python.el
> > @@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
> >             "staticmethod" "str" "sum" "super" "tuple" "type"
> > "vars" "zip"
> >             "__import__"
> >             ;; Python 2:
> > -           "basestring" "cmp" "execfile" "file" "long" "raw_input"
> > "reduce"
> > +           "basestring" "cmp" "execfile" "intern" "long"
> > "raw_input" "reduce"
> >             "reload" "unichr" "unicode" "xrange" "apply" "buffer"
> > "coerce"
> > -           "intern"
> 
> I understand that "file" probably the top most common one, but,
> looking
> at the above list of builtins, many of the remaining ones should be
> attractive names to use as well:
> 
>     apply
>     basestring
>     buffer
>     cmp
>     coerce
>     execfile
>     file
>     intern
>     long
>     raw_input
>     reduce
>     reload
>     unichr
>     unicode
>     xrange

Not that I'm against, but I'd point out that to me basestring, coerce,
execfile, intern, long, raw_input, reload, unichr, unicode, xrange look
like highly unlikely variable names. "long" as I mentioned above would
typically be part of a larger word. "buffer" I also don't recall people
to write ever in full (people are more likely to type "buf").

And `reduce` wasn't really removed from Python3 (it's just not in the
visibility scope by default), so it seems odd to remove given that we
still highlight `all` and `any`.

> If we're anyways making changes here, maybe we should just take the
> plunge and drop all of the Python 2 builtins?  It should be
> relatively
> harmless for Python 2 users, I think, and it's easier to explain what
> we're doing in etc/NEWS.




^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2] Remove Python2 "file" from the list of keywords
  2025-01-30  7:07                         ` Konstantin Kharlamov
@ 2025-01-30 15:15                           ` kobarity
  2025-01-30 19:00                             ` [PATCH v3] Stop highlighting Python2 keywords Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-01-30 15:15 UTC (permalink / raw)
  To: Konstantin Kharlamov
  Cc: Stefan Kangas, Ship Mints, Kévin Le Gouguec, emacs-devel

Konstantin Kharlamov wrote:
> 
> On Wed, 2025-01-29 at 19:56 -0600, Stefan Kangas wrote:
> > Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> > 
> > > On Wed, 2025-01-29 at 23:26 +0900, kobarity wrote:
> > > > Konstantin Kharlamov wrote:
> > > > > So, it's been a few days, nobody seems against, so I'm
> > > > > attaching a
> > > > > patch. Please review 😊
> > > > 
> > > > I think it should be announced in etc/NEWS.
> > > 
> > > Thank you, done!
> > > From ffda4e954d157b544453b388458f1ec414d17abd Mon Sep 17 00:00:00
> > > 2001
> > > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > > Date: Wed, 29 Jan 2025 08:35:31 +0300
> > > Subject: [PATCH v2] Remove Python2 "file" from the list of keywords
> > > 
> > > `file` was a built-in type in Python2, removed in Python3. 
> > > Nowadays,
> > > word `file` is a frequent identifier name that people use in short
> > > file-related code sections.  Having it highlighted looks odd in a
> > > code.  As a prior art, Vim and VS Code don't currently highlight
> > > `file` specially.
> > > 
> > > This was recently discussed on emacs-devel with the conclusion it
> > > is
> > > okay to remove `file` from the list of keywords.
> > > 
> > > * etc/NEWS: Announce "file" keyword removal.
> > > * lisp/progmodes/python.el (python-font-lock-keywords-level-2):
> > > Remove
> > > "file" from the list.
> > > ---
> > >  etc/NEWS                 | 6 ++++++
> > >  lisp/progmodes/python.el | 3 +--
> > >  2 files changed, 7 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/etc/NEWS b/etc/NEWS
> > > index f7e9f283709..ca16079ad64 100644
> > > --- a/etc/NEWS
> > > +++ b/etc/NEWS
> > > @@ -807,6 +807,12 @@ exist.  If "python" points to Python 2 on your
> > > system, you now have to
> > >  customize these variables to "python3" if you want to use Python 3
> > >  instead.
> > > 
> > > +---
> > > +*** Remove "file" from the list of keywords.
> > > +In Python 3 "file" has no special meaning, and in modern code the
> > > word
> > > +is frequently used as a variable name.  To make sure highlight in
> > > such
> > > +code doesn't look off, we no longer highlight "file".
> > > +

Thanks.  If possible, it would be good to write this so that it is
clear that this change is 'python-mode' specific and does not pertain
to 'python-ts-mode'.

> > >  ---
> > >  *** Support of 'electric-layout-mode' added.
> > > 
> > > diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
> > > index c00de2d6a8d..63798687928 100644
> > > --- a/lisp/progmodes/python.el
> > > +++ b/lisp/progmodes/python.el
> > > @@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
> > >             "staticmethod" "str" "sum" "super" "tuple" "type"
> > > "vars" "zip"
> > >             "__import__"
> > >             ;; Python 2:
> > > -           "basestring" "cmp" "execfile" "file" "long" "raw_input"
> > > "reduce"
> > > +           "basestring" "cmp" "execfile" "intern" "long"
> > > "raw_input" "reduce"
> > >             "reload" "unichr" "unicode" "xrange" "apply" "buffer"
> > > "coerce"
> > > -           "intern"
> > 
> > I understand that "file" probably the top most common one, but,
> > looking
> > at the above list of builtins, many of the remaining ones should be
> > attractive names to use as well:
> > 
> >     apply
> >     basestring
> >     buffer
> >     cmp
> >     coerce
> >     execfile
> >     file
> >     intern
> >     long
> >     raw_input
> >     reduce
> >     reload
> >     unichr
> >     unicode
> >     xrange
> 
> Not that I'm against, but I'd point out that to me basestring, coerce,
> execfile, intern, long, raw_input, reload, unichr, unicode, xrange look
> like highly unlikely variable names. "long" as I mentioned above would
> typically be part of a larger word. "buffer" I also don't recall people
> to write ever in full (people are more likely to type "buf").

I'm not against dropping all the Python 2 builtins either.

> And `reduce` wasn't really removed from Python3 (it's just not in the
> visibility scope by default), so it seems odd to remove given that we
> still highlight `all` and `any`.

Do you mean that we can import as follows?

#+begin_src python
from functools import reduce
#+end_src

I don't think it is a builtin.  There needs to be some criteria.

It might be nice to have a `defcustom' for the list of words to
highlight as builtins, but it would be a bit complicated to introduce
while maintaining compatibility with the current
`python-font-lock-keywords-level-2',
`python-font-lock-keywords-maximum-decoration', and
`python-font-lock-keywords'.

> > If we're anyways making changes here, maybe we should just take the
> > plunge and drop all of the Python 2 builtins?  It should be
> > relatively
> > harmless for Python 2 users, I think, and it's easier to explain what
> > we're doing in etc/NEWS.

I don't have a strong preference for either, but I agree that it is
easier to explain if all of the Python 2 builtins are dropped.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-01-30 15:15                           ` kobarity
@ 2025-01-30 19:00                             ` Konstantin Kharlamov
  2025-01-31  0:45                               ` Stefan Kangas
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-01-30 19:00 UTC (permalink / raw)
  To: kobarity; +Cc: Stefan Kangas, Ship Mints, Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 6270 bytes --]

On Fri, 2025-01-31 at 00:15 +0900, kobarity wrote:
> Konstantin Kharlamov wrote:
> > 
> > On Wed, 2025-01-29 at 19:56 -0600, Stefan Kangas wrote:
> > > Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> > > 
> > > > On Wed, 2025-01-29 at 23:26 +0900, kobarity wrote:
> > > > > Konstantin Kharlamov wrote:
> > > > > > So, it's been a few days, nobody seems against, so I'm
> > > > > > attaching a
> > > > > > patch. Please review 😊
> > > > > 
> > > > > I think it should be announced in etc/NEWS.
> > > > 
> > > > Thank you, done!
> > > > From ffda4e954d157b544453b388458f1ec414d17abd Mon Sep 17
> > > > 00:00:00
> > > > 2001
> > > > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > > > Date: Wed, 29 Jan 2025 08:35:31 +0300
> > > > Subject: [PATCH v2] Remove Python2 "file" from the list of
> > > > keywords
> > > > 
> > > > `file` was a built-in type in Python2, removed in Python3. 
> > > > Nowadays,
> > > > word `file` is a frequent identifier name that people use in
> > > > short
> > > > file-related code sections.  Having it highlighted looks odd in
> > > > a
> > > > code.  As a prior art, Vim and VS Code don't currently
> > > > highlight
> > > > `file` specially.
> > > > 
> > > > This was recently discussed on emacs-devel with the conclusion
> > > > it
> > > > is
> > > > okay to remove `file` from the list of keywords.
> > > > 
> > > > * etc/NEWS: Announce "file" keyword removal.
> > > > * lisp/progmodes/python.el (python-font-lock-keywords-level-2):
> > > > Remove
> > > > "file" from the list.
> > > > ---
> > > >  etc/NEWS                 | 6 ++++++
> > > >  lisp/progmodes/python.el | 3 +--
> > > >  2 files changed, 7 insertions(+), 2 deletions(-)
> > > > 
> > > > diff --git a/etc/NEWS b/etc/NEWS
> > > > index f7e9f283709..ca16079ad64 100644
> > > > --- a/etc/NEWS
> > > > +++ b/etc/NEWS
> > > > @@ -807,6 +807,12 @@ exist.  If "python" points to Python 2 on
> > > > your
> > > > system, you now have to
> > > >  customize these variables to "python3" if you want to use
> > > > Python 3
> > > >  instead.
> > > > 
> > > > +---
> > > > +*** Remove "file" from the list of keywords.
> > > > +In Python 3 "file" has no special meaning, and in modern code
> > > > the
> > > > word
> > > > +is frequently used as a variable name.  To make sure highlight
> > > > in
> > > > such
> > > > +code doesn't look off, we no longer highlight "file".
> > > > +
> 
> Thanks.  If possible, it would be good to write this so that it is
> clear that this change is 'python-mode' specific and does not pertain
> to 'python-ts-mode'.
> 
> > > >  ---
> > > >  *** Support of 'electric-layout-mode' added.
> > > > 
> > > > diff --git a/lisp/progmodes/python.el
> > > > b/lisp/progmodes/python.el
> > > > index c00de2d6a8d..63798687928 100644
> > > > --- a/lisp/progmodes/python.el
> > > > +++ b/lisp/progmodes/python.el
> > > > @@ -715,9 +715,8 @@ python-font-lock-keywords-level-2
> > > >             "staticmethod" "str" "sum" "super" "tuple" "type"
> > > > "vars" "zip"
> > > >             "__import__"
> > > >             ;; Python 2:
> > > > -           "basestring" "cmp" "execfile" "file" "long"
> > > > "raw_input"
> > > > "reduce"
> > > > +           "basestring" "cmp" "execfile" "intern" "long"
> > > > "raw_input" "reduce"
> > > >             "reload" "unichr" "unicode" "xrange" "apply"
> > > > "buffer"
> > > > "coerce"
> > > > -           "intern"
> > > 
> > > I understand that "file" probably the top most common one, but,
> > > looking
> > > at the above list of builtins, many of the remaining ones should
> > > be
> > > attractive names to use as well:
> > > 
> > >     apply
> > >     basestring
> > >     buffer
> > >     cmp
> > >     coerce
> > >     execfile
> > >     file
> > >     intern
> > >     long
> > >     raw_input
> > >     reduce
> > >     reload
> > >     unichr
> > >     unicode
> > >     xrange
> > 
> > Not that I'm against, but I'd point out that to me basestring,
> > coerce,
> > execfile, intern, long, raw_input, reload, unichr, unicode, xrange
> > look
> > like highly unlikely variable names. "long" as I mentioned above
> > would
> > typically be part of a larger word. "buffer" I also don't recall
> > people
> > to write ever in full (people are more likely to type "buf").
> 
> I'm not against dropping all the Python 2 builtins either.
> 
> > And `reduce` wasn't really removed from Python3 (it's just not in
> > the
> > visibility scope by default), so it seems odd to remove given that
> > we
> > still highlight `all` and `any`.
> 
> Do you mean that we can import as follows?
> 
> #+begin_src python
> from functools import reduce
> #+end_src
> 
> I don't think it is a builtin.  There needs to be some criteria.
> 
> It might be nice to have a `defcustom' for the list of words to
> highlight as builtins, but it would be a bit complicated to introduce
> while maintaining compatibility with the current
> `python-font-lock-keywords-level-2',
> `python-font-lock-keywords-maximum-decoration', and
> `python-font-lock-keywords'.
> 
> > > If we're anyways making changes here, maybe we should just take
> > > the
> > > plunge and drop all of the Python 2 builtins?  It should be
> > > relatively
> > > harmless for Python 2 users, I think, and it's easier to explain
> > > what
> > > we're doing in etc/NEWS.
> 
> I don't have a strong preference for either, but I agree that it is
> easier to explain if all of the Python 2 builtins are dropped.

Okay, if you folks want, let's remove them all 😊 I don't have a
preference.

Attaching a patch, please see if it's okay. I'd like to point out the
code still has a `StandardError` Python 2 exception, should we drop it
as well then? I was about to do that, but then I realized this one
appears also in the python-ts-mode declared in the same file. I'm just
unsure how to declare it in the news then (like, is it "we remove all
python2 keywords from python-mode and StandardError from python-ts-
mode"?), and if it's even desirable?

[-- Attachment #2: 1.patch --]
[-- Type: text/x-patch, Size: 2274 bytes --]

From ec7090231e105d63b4c132233686ec2d8a3ac00a Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Date: Wed, 29 Jan 2025 08:35:31 +0300
Subject: [PATCH v3] Stop highlighting Python2 keywords.

Many of the keywords may appear as an identifier in Python 3 code, and
it's an odd user experience to see one being highlighted as a keyword.
Since Python 2 EOL has happened 5 years ago, this commit removes
Python 2 keywords from the highlight to improve user experience of the
modern Python users.

This patch is result of a discussion on emacs-devel.

* etc/NEWS: Announce "keywords removal.
* lisp/progmodes/python.el (python-font-lock-keywords-level-2): Remove
Python 2 keywords from the list.
---
 etc/NEWS                 | 8 ++++++++
 lisp/progmodes/python.el | 4 ----
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index f7e9f283709..0e0856dfcb9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -807,6 +807,14 @@ exist.  If "python" points to Python 2 on your system, you now have to
 customize these variables to "python3" if you want to use Python 3
 instead.
 
+---
+*** Remove Python 2 keywords from 'python-mode'.
+Many of the keywords may appear as an identifier in Python 3 code, and
+it's an odd user experience to see one being highlighted as a keyword.
+Since Python 2 EOL has happened 5 years ago, this release removes Python
+2 keywords from the highlight to improve user experience of the modern
+Python users.
+
 ---
 *** Support of 'electric-layout-mode' added.
 
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index c00de2d6a8d..e4588b4ac09 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -714,10 +714,6 @@ python-font-lock-keywords-level-2
            "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
            "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
            "__import__"
-           ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
-           "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
            ;; Python 3:
            "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
            ;; Special attributes:
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-01-30 19:00                             ` [PATCH v3] Stop highlighting Python2 keywords Konstantin Kharlamov
@ 2025-01-31  0:45                               ` Stefan Kangas
  2025-01-31 14:53                                 ` kobarity
  2025-02-01 20:09                                 ` Richard Stallman
  0 siblings, 2 replies; 35+ messages in thread
From: Stefan Kangas @ 2025-01-31  0:45 UTC (permalink / raw)
  To: Konstantin Kharlamov, kobarity
  Cc: Ship Mints, Kévin Le Gouguec, emacs-devel

Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:

>> I'm not against dropping all the Python 2 builtins either.
[...]
>> I don't have a strong preference for either, but I agree that it is
>> easier to explain if all of the Python 2 builtins are dropped.
>
> Okay, if you folks want, let's remove them all 😊 I don't have a
> preference.

I have an idea that might be gentler for folks still in need of Python 2
support: We add a new defcustom `python-2-support`, and then disable all
the Python 2 stuff unless that variable is customized to t.

At this point, I think we could set it to nil by default, but I don't
have a very strong preference.  We could also do that after Emacs 31, if
other people think that is better.  The main reason why I'd lean towards
setting it to nil already in Emacs 30 is to avoid confusion for the 94%
of users that are now using Python 3.

Then at some point in the future, when Python 2 support is irrelevant or
close to it, we just make that variable obsolete.

How does that sound?

> I'd like to point out the code still has a `StandardError` Python 2
> exception, should we drop it as well then?

I think "StandardError" could be covered by the same user option.

> I was about to do that, but then I realized this one appears also in
> the python-ts-mode declared in the same file.

AFAICT, the tree-sitter grammar supports both Python 2 and 3:
https://github.com/tree-sitter/tree-sitter-python



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-01-31  0:45                               ` Stefan Kangas
@ 2025-01-31 14:53                                 ` kobarity
  2025-02-02 15:21                                   ` kobarity
  2025-02-01 20:09                                 ` Richard Stallman
  1 sibling, 1 reply; 35+ messages in thread
From: kobarity @ 2025-01-31 14:53 UTC (permalink / raw)
  To: Stefan Kangas
  Cc: Konstantin Kharlamov, Ship Mints, Kévin Le Gouguec,
	emacs-devel

Stefan Kangas wrote:
> 
> Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> 
> >> I'm not against dropping all the Python 2 builtins either.
> [...]
> >> I don't have a strong preference for either, but I agree that it is
> >> easier to explain if all of the Python 2 builtins are dropped.
> >
> > Okay, if you folks want, let's remove them all 😊 I don't have a
> > preference.
> 
> I have an idea that might be gentler for folks still in need of Python 2
> support: We add a new defcustom `python-2-support`, and then disable all
> the Python 2 stuff unless that variable is customized to t.

I think such defcustom would be good, but it might be a bit cumbersome
to reflect the change to `python-font-lock-keywords-level-2' and
`python-font-lock-keywords', etc.  Would it be acceptable to save the
changes and then restart Emacs?

If it is acceptable, we can also have a `defcustom' for the list of
words to highlight as builtins.

> At this point, I think we could set it to nil by default, but I don't
> have a very strong preference.  We could also do that after Emacs 31, if
> other people think that is better.  The main reason why I'd lean towards
> setting it to nil already in Emacs 30 is to avoid confusion for the 94%
> of users that are now using Python 3.

I agree with defaulting to nil.

> Then at some point in the future, when Python 2 support is irrelevant or
> close to it, we just make that variable obsolete.
> 
> How does that sound?
> 
> > I'd like to point out the code still has a `StandardError` Python 2
> > exception, should we drop it as well then?
> 
> I think "StandardError" could be covered by the same user option.
> 
> > I was about to do that, but then I realized this one appears also in
> > the python-ts-mode declared in the same file.
> 
> AFAICT, the tree-sitter grammar supports both Python 2 and 3:
> https://github.com/tree-sitter/tree-sitter-python

I believe tree-sitter itself supports both, but
`python--treesit-builtins' only has Python 3 builtins.  So I imagine
that StandardError may have been accidentally mixed in.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-01-31  0:45                               ` Stefan Kangas
  2025-01-31 14:53                                 ` kobarity
@ 2025-02-01 20:09                                 ` Richard Stallman
  2025-02-01 21:17                                   ` Gabriel Santos
  2025-02-02 15:35                                   ` Stefan Kangas
  1 sibling, 2 replies; 35+ messages in thread
From: Richard Stallman @ 2025-02-01 20:09 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

Python 2 and Python 3 seem rather different, and it seems that the
same mode is being used for both.

Would it be reasonable to make two slightly different modes for them,
so as to highlight each language version as is best for that version?

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-01 20:09                                 ` Richard Stallman
@ 2025-02-01 21:17                                   ` Gabriel Santos
  2025-02-02 15:35                                   ` Stefan Kangas
  1 sibling, 0 replies; 35+ messages in thread
From: Gabriel Santos @ 2025-02-01 21:17 UTC (permalink / raw)
  To: rms, Richard Stallman, Stefan Kangas; +Cc: emacs-devel

>Would it be reasonable to make two slightly different modes for them,
>so as to highlight each language version as is best for that version?

I think so.

The way the Python modes currently work is that there's a main python-base-mode,
with python-mode and python-ts-mode deriving from it.
So maybe we could have a python2-mode.

--
Gabriel Santos



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-01-31 14:53                                 ` kobarity
@ 2025-02-02 15:21                                   ` kobarity
  2025-02-02 15:43                                     ` Stefan Kangas
  2025-02-02 22:54                                     ` Konstantin Kharlamov
  0 siblings, 2 replies; 35+ messages in thread
From: kobarity @ 2025-02-02 15:21 UTC (permalink / raw)
  To: Stefan Kangas, Konstantin Kharlamov, Ship Mints,
	Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2717 bytes --]

kobarity wrote:
> 
> Stefan Kangas wrote:
> > 
> > Konstantin Kharlamov <Hi-Angel@yandex.ru> writes:
> > 
> > >> I'm not against dropping all the Python 2 builtins either.
> > [...]
> > >> I don't have a strong preference for either, but I agree that it is
> > >> easier to explain if all of the Python 2 builtins are dropped.
> > >
> > > Okay, if you folks want, let's remove them all 😊 I don't have a
> > > preference.
> > 
> > I have an idea that might be gentler for folks still in need of Python 2
> > support: We add a new defcustom `python-2-support`, and then disable all
> > the Python 2 stuff unless that variable is customized to t.
> 
> I think such defcustom would be good, but it might be a bit cumbersome
> to reflect the change to `python-font-lock-keywords-level-2' and
> `python-font-lock-keywords', etc.  Would it be acceptable to save the
> changes and then restart Emacs?
> 
> If it is acceptable, we can also have a `defcustom' for the list of
> words to highlight as builtins.
> 
> > At this point, I think we could set it to nil by default, but I don't
> > have a very strong preference.  We could also do that after Emacs 31, if
> > other people think that is better.  The main reason why I'd lean towards
> > setting it to nil already in Emacs 30 is to avoid confusion for the 94%
> > of users that are now using Python 3.
> 
> I agree with defaulting to nil.
> 
> > Then at some point in the future, when Python 2 support is irrelevant or
> > close to it, we just make that variable obsolete.
> > 
> > How does that sound?
> > 
> > > I'd like to point out the code still has a `StandardError` Python 2
> > > exception, should we drop it as well then?
> > 
> > I think "StandardError" could be covered by the same user option.
> > 
> > > I was about to do that, but then I realized this one appears also in
> > > the python-ts-mode declared in the same file.
> > 
> > AFAICT, the tree-sitter grammar supports both Python 2 and 3:
> > https://github.com/tree-sitter/tree-sitter-python
> 
> I believe tree-sitter itself supports both, but
> `python--treesit-builtins' only has Python 3 builtins.  So I imagine
> that StandardError may have been accidentally mixed in.

I'm attaching a patch as a reference for discussion.  This patch
introduces a new defcustom `python-2-support'.  You must save and
restart Emacs for it to work.  If `python-2-support' is non-nil,
Python 2 only builtins are highlighted in `python-ts-mode' as well as
`python-mode'.  I thought about adding a defcustom of the list of
words to highlight as well, but I think it is enough to override
`python-font-lock-builtins-python3' etc. by `setq' in init.el.

[-- Attachment #2: 0001-Make-it-configurable-to-highlight-Python-2-builtins.patch --]
[-- Type: application/octet-stream, Size: 13771 bytes --]

From ca4ce599676177b55d95b7ebe6fb016096b68366 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Mon, 3 Feb 2025 00:02:42 +0900
Subject: [PATCH] Make it configurable to highlight Python 2 builtins

By default, Python 2 only builtins are not highlighted.

* lisp/progmodes/python.el (python-2-support): New defcustom.
(python-font-lock-builtin-types)
(python-font-lock-builtins-python3)
(python-font-lock-builtins-python2)
(python-font-lock-builtins)
(python-font-lock-special-attributes)
(python-font-lock-builtin-exceptions-python3)
(python-font-lock-builtin-exceptions-python2)
(python-font-lock-builtin-exceptions): New variables.
(python-font-lock-keywords-level-2)
(python-font-lock-keywords-maximum-decoration)
(python--treesit-builtin-types)
(python--treesit-builtins)
(python--treesit-special-attributes)
(python--treesit-exceptions): Use new variables.

Co-authored-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
---
 etc/NEWS                 |   7 ++
 lisp/progmodes/python.el | 197 ++++++++++++++++++---------------------
 2 files changed, 98 insertions(+), 106 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 259b6e03549..74fb2fb238c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -956,6 +956,13 @@ exist.  If "python" points to Python 2 on your system, you now have to
 customize these variables to "python3" if you want to use Python 3
 instead.
 
+---
+*** New user option 'python-2-support'.
+By default, Python 2 only builtins such as "file" are not highlighted.
+If you would like to highlight them, set this option to non-nil and
+restart Emacs.  If this option is non-nil, Python 2 only builtins will
+be highlighted in 'python-ts-mode' as well as 'python-mode'.
+
 ---
 *** Support of 'electric-layout-mode' added.
 
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 3a297a3979c..081b532ab04 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -316,6 +316,16 @@ python-interpreter-args
   :version "30.1"
   :type 'string)
 
+(defcustom python-2-support nil
+  "Whether to enable Python 2 support.
+If non-nil, enable Python 2 support.  Currently only affects
+highlighting.
+
+This variable only works after saving and restarting Emacs."
+  :version "31.1"
+  :type 'boolean
+  :safe 'booleanp)
+
 \f
 ;;; Bindings
 
@@ -689,6 +699,40 @@ python-font-lock-keywords-level-1
 This is the minimum decoration level, including function and
 class declarations.")
 
+(defvar python-font-lock-builtin-types
+  '("bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset"
+    "int" "list" "memoryview" "range" "set" "str" "tuple"))
+
+(defvar python-font-lock-builtins-python3
+  '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
+    "callable" "chr" "classmethod" "compile" "delattr" "dir" "divmod"
+    "enumerate" "eval" "exec" "filter" "format" "getattr" "globals"
+    "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
+    "issubclass" "iter" "len" "locals" "map" "max" "min" "next"
+    "object" "oct" "open" "ord" "pow" "print" "property" "repr"
+    "reversed" "round" "setattr" "slice" "sorted" "staticmethod" "sum"
+    "super" "type" "vars" "zip" "__import__"))
+
+(defvar python-font-lock-builtins-python2
+  '("basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
+    "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
+    "intern"))
+
+(defvar python-font-lock-builtins
+  (append python-font-lock-builtins-python3
+          (when python-2-support
+            python-font-lock-builtins-python2)))
+
+(defvar python-font-lock-special-attributes
+  '(;; https://docs.python.org/3/reference/datamodel.html
+    "__annotations__" "__bases__" "__closure__" "__code__"
+    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
+    "__globals__" "__kwdefaults__" "__name__" "__module__"
+    "__mro__" "__package__" "__qualname__"
+    "__static_attributes__" "__type_params__"
+    ;; Extras:
+    "__all__"))
+
 (defvar python-font-lock-keywords-level-2
   `(,@python-font-lock-keywords-level-1
     ,(rx symbol-start
@@ -711,33 +755,11 @@ python-font-lock-keywords-level-2
           "self")
          symbol-end)
     ;; Builtins
-    (,(rx symbol-start
-          (or
-           "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
-           "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
-           "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
-           "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
-           "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
-           "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
-           "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
-           "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
-           "__import__"
-           ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
-           "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
-           ;; Python 3:
-           "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
-           ;; Special attributes:
-           ;; https://docs.python.org/3/reference/datamodel.html
-           "__annotations__" "__bases__" "__closure__" "__code__"
-           "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-           "__globals__" "__kwdefaults__" "__name__" "__module__"
-           "__mro__" "__package__" "__qualname__"
-           "__static_attributes__" "__type_params__"
-           ;; Extras:
-           "__all__")
-          symbol-end) . font-lock-builtin-face))
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@(append python-font-lock-builtin-types
+                                        python-font-lock-builtins
+                                        python-font-lock-special-attributes))
+                          symbol-end)) . font-lock-builtin-face))
   "Font lock keywords to use in `python-mode' for level 2 decoration.
 
 This is the medium decoration level, including everything in
@@ -759,6 +781,41 @@ python-font-lock-assignment-matcher
                         (equal (char-after) ?=))
                return (progn (backward-char) t))))
 
+(defvar python-font-lock-builtin-exceptions-python3
+  '(;; Python 2 and 3:
+    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
+    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
+    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
+    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
+    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
+    "LookupError" "MemoryError" "NameError" "NotImplementedError"
+    "OSError" "OverflowError" "PendingDeprecationWarning"
+    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
+    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
+    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
+    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
+    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
+    "ZeroDivisionError"
+    ;; Python 3:
+    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
+    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
+    "ConnectionResetError" "EncodingWarning" "FileExistsError"
+    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
+    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
+    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
+    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
+    "BaseExceptionGroup" "ExceptionGroup"
+    ;; OS specific
+    "VMSError" "WindowsError"))
+
+(defvar python-font-lock-builtin-exceptions-python2
+  '("StandardError"))
+
+(defvar python-font-lock-builtin-exceptions
+  (append python-font-lock-builtin-exceptions-python3
+          (when python-2-support
+            python-font-lock-builtin-exceptions-python2)))
+
 (defvar python-font-lock-keywords-maximum-decoration
   `((python--font-lock-f-strings)
     ,@python-font-lock-keywords-level-2
@@ -776,38 +833,9 @@ python-font-lock-keywords-maximum-decoration
                                             (0+ "." (1+ (or word ?_)))))
      (1 font-lock-type-face))
     ;; Builtin Exceptions
-    (,(rx symbol-start
-          (or
-           ;; Python 2 and 3:
-           "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-           "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-           "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-           "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-           "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-           "LookupError" "MemoryError" "NameError" "NotImplementedError"
-           "OSError" "OverflowError" "PendingDeprecationWarning"
-           "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-           "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-           "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-           "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-           "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-           "ZeroDivisionError"
-           ;; Python 2:
-           "StandardError"
-           ;; Python 3:
-           "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-           "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-           "ConnectionResetError" "EncodingWarning" "FileExistsError"
-           "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-           "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-           "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-           "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-           "BaseExceptionGroup" "ExceptionGroup"
-           ;; OS specific
-           "VMSError" "WindowsError"
-           )
-          symbol-end)
-     . font-lock-type-face)
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@python-font-lock-builtin-exceptions)
+                          symbol-end)) . font-lock-type-face)
     ;; single assignment with/without type hints, e.g.
     ;;   a: int = 5
     ;;   b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo')
@@ -1015,8 +1043,7 @@ python--treesit-keywords
     "and" "in" "is" "not" "or" "not in" "is not"))
 
 (defvar python--treesit-builtin-types
-  '("int" "float" "complex" "bool" "list" "tuple" "range" "str"
-    "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
+  python-font-lock-builtin-types)
 
 (defvar python--treesit-type-regex
   (rx-to-string `(seq bol (or
@@ -1025,17 +1052,7 @@ python--treesit-type-regex
                   eol)))
 
 (defvar python--treesit-builtins
-  (append python--treesit-builtin-types
-          '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
-            "callable" "chr" "classmethod" "compile"
-            "delattr" "dir" "divmod" "enumerate" "eval" "exec"
-            "filter" "format" "getattr" "globals"
-            "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
-            "issubclass" "iter" "len" "locals" "map" "max"
-            "min" "next" "object" "oct" "open" "ord" "pow"
-            "print" "property" "repr" "reversed" "round"
-            "setattr" "slice" "sorted" "staticmethod" "sum" "super"
-            "type" "vars" "zip" "__import__")))
+  python-font-lock-builtins)
 
 (defvar python--treesit-constants
   '("Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
@@ -1047,42 +1064,10 @@ python--treesit-operators
     ">>" ">>=" "|" "|=" "~" "@" "@="))
 
 (defvar python--treesit-special-attributes
-  '("__annotations__" "__bases__" "__closure__" "__code__"
-    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-    "__globals__" "__kwdefaults__" "__name__" "__module__"
-    "__mro__" "__package__" "__qualname__"
-    "__static_attributes__" "__type_params__"
-    "__all__"))
+  python-font-lock-special-attributes)
 
 (defvar python--treesit-exceptions
-  '(;; Python 2 and 3:
-    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-    "LookupError" "MemoryError" "NameError" "NotImplementedError"
-    "OSError" "OverflowError" "PendingDeprecationWarning"
-    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-    "ZeroDivisionError"
-    ;; Python 2:
-    "StandardError"
-    ;; Python 3:
-    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-    "ConnectionResetError" "EncodingWarning" "FileExistsError"
-    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-    "BaseExceptionGroup" "ExceptionGroup"
-    ;; OS specific
-    "VMSError" "WindowsError"
-    ))
+  python-font-lock-builtin-exceptions)
 
 (defun python--treesit-fontify-string (node override start end &rest _)
   "Fontify string.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-01 20:09                                 ` Richard Stallman
  2025-02-01 21:17                                   ` Gabriel Santos
@ 2025-02-02 15:35                                   ` Stefan Kangas
  1 sibling, 0 replies; 35+ messages in thread
From: Stefan Kangas @ 2025-02-02 15:35 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

Richard Stallman <rms@gnu.org> writes:

> Python 2 and Python 3 seem rather different, and it seems that the
> same mode is being used for both.
>
> Would it be reasonable to make two slightly different modes for them,
> so as to highlight each language version as is best for that version?

No objections from me.  However, it seems that it would require a fair
bit more work, including documentation changes, so someone would have to
volunteer to do that.

The benefit of the `python-2-support` user option is that it is simpler
to implement.  It should also be slightly easier to use, given that it
would not require changes to auto-mode-alist or interpreter-mode-regexp.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-02 15:21                                   ` kobarity
@ 2025-02-02 15:43                                     ` Stefan Kangas
  2025-02-03 14:54                                       ` kobarity
  2025-02-02 22:54                                     ` Konstantin Kharlamov
  1 sibling, 1 reply; 35+ messages in thread
From: Stefan Kangas @ 2025-02-02 15:43 UTC (permalink / raw)
  To: kobarity, Konstantin Kharlamov, Ship Mints, Kévin Le Gouguec,
	emacs-devel

kobarity <kobarity@gmail.com> writes:

> I'm attaching a patch as a reference for discussion.  This patch
> introduces a new defcustom `python-2-support'.  You must save and
> restart Emacs for it to work.  If `python-2-support' is non-nil,
> Python 2 only builtins are highlighted in `python-ts-mode' as well as
> `python-mode'.

Thanks, ideally we would not require restart but only `setopt`, but
that's not a blocker.  It shouldn't be too hard to implement later, but
would require functions that set up these variables.

> I thought about adding a defcustom of the list of words to highlight
> as well, but I think it is enough to override
> `python-font-lock-builtins-python3' etc. by `setq' in init.el.

Makes sense to me.

> From ca4ce599676177b55d95b7ebe6fb016096b68366 Mon Sep 17 00:00:00 2001
> From: kobarity <kobarity@gmail.com>
> Date: Mon, 3 Feb 2025 00:02:42 +0900
> Subject: [PATCH] Make it configurable to highlight Python 2 builtins
>
> By default, Python 2 only builtins are not highlighted.
>
> * lisp/progmodes/python.el (python-2-support): New defcustom.
> (python-font-lock-builtin-types)
> (python-font-lock-builtins-python3)
> (python-font-lock-builtins-python2)
> (python-font-lock-builtins)
> (python-font-lock-special-attributes)
> (python-font-lock-builtin-exceptions-python3)
> (python-font-lock-builtin-exceptions-python2)
> (python-font-lock-builtin-exceptions): New variables.
> (python-font-lock-keywords-level-2)
> (python-font-lock-keywords-maximum-decoration)
> (python--treesit-builtin-types)
> (python--treesit-builtins)
> (python--treesit-special-attributes)
> (python--treesit-exceptions): Use new variables.
>
> Co-authored-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> ---
>  etc/NEWS                 |   7 ++
>  lisp/progmodes/python.el | 197 ++++++++++++++++++---------------------
>  2 files changed, 98 insertions(+), 106 deletions(-)
>
> diff --git a/etc/NEWS b/etc/NEWS
> index 259b6e03549..74fb2fb238c 100644
> --- a/etc/NEWS
> +++ b/etc/NEWS
> @@ -956,6 +956,13 @@ exist.  If "python" points to Python 2 on your system, you now have to
>  customize these variables to "python3" if you want to use Python 3
>  instead.
>
> +---
> +*** New user option 'python-2-support'.
> +By default, Python 2 only builtins such as "file" are not highlighted.
               ^^^^^^^^^^^^^

I believe this should be

    Python 2-only

> +If you would like to highlight them, set this option to non-nil and
                                        ^^^

customize

> +restart Emacs.  If this option is non-nil, Python 2 only builtins will
> +be highlighted in 'python-ts-mode' as well as 'python-mode'.

I couldn't see anything else that stood out in the patch in cursory
reading, and I also didn't test it.  If you think it's good, I'd install
it, and we can do further refinements later.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-02 15:21                                   ` kobarity
  2025-02-02 15:43                                     ` Stefan Kangas
@ 2025-02-02 22:54                                     ` Konstantin Kharlamov
  1 sibling, 0 replies; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-02-02 22:54 UTC (permalink / raw)
  To: kobarity, Stefan Kangas, Ship Mints, Kévin Le Gouguec,
	emacs-devel

On Mon, 2025-02-03 at 00:21 +0900, kobarity wrote:
> I'm attaching a patch as a reference for discussion.  This patch
> introduces a new defcustom `python-2-support'.  You must save and
> restart Emacs for it to work.  If `python-2-support' is non-nil,
> Python 2 only builtins are highlighted in `python-ts-mode' as well as
> `python-mode'.  I thought about adding a defcustom of the list of
> words to highlight as well, but I think it is enough to override
> `python-font-lock-builtins-python3' etc. by `setq' in init.el.

Thank you!

> From ca4ce599676177b55d95b7ebe6fb016096b68366 Mon Sep 17 00:00:00 2001
> From: kobarity <kobarity@gmail.com>
> Date: Mon, 3 Feb 2025 00:02:42 +0900
> Subject: [PATCH] Make it configurable to highlight Python 2 builtins
>
> By default, Python 2 only builtins are not highlighted.
>
> * lisp/progmodes/python.el (python-2-support): New defcustom.
> (python-font-lock-builtin-types)
> (python-font-lock-builtins-python3)
> (python-font-lock-builtins-python2)
> (python-font-lock-builtins)
> (python-font-lock-special-attributes)
> (python-font-lock-builtin-exceptions-python3)
> (python-font-lock-builtin-exceptions-python2)
> (python-font-lock-builtin-exceptions): New variables.
> (python-font-lock-keywords-level-2)
> (python-font-lock-keywords-maximum-decoration)
> (python--treesit-builtin-types)
> (python--treesit-builtins)
> (python--treesit-special-attributes)
> (python--treesit-exceptions): Use new variables.
>
> Co-authored-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> ---
>  etc/NEWS                 |   7 ++
>  lisp/progmodes/python.el | 197 ++++++++++++++++++---------------------
>  2 files changed, 98 insertions(+), 106 deletions(-)
>
> diff --git a/etc/NEWS b/etc/NEWS
> index 259b6e03549..74fb2fb238c 100644
> --- a/etc/NEWS
> +++ b/etc/NEWS
> @@ -956,6 +956,13 @@ exist.  If "python" points to Python 2 on your
> system, you now have to
>  customize these variables to "python3" if you want to use Python 3
>  instead.
>
> +---
> +*** New user option 'python-2-support'.
> +By default, Python 2 only builtins such as "file" are not highlighted.
> +If you would like to highlight them, set this option to non-nil and
> +restart Emacs.  If this option is non-nil, Python 2 only builtins will
> +be highlighted in 'python-ts-mode' as well as 'python-mode'.

It seems it would be nice to mention explicitly why the change is done,
i.e. that highlighting python2-only identifiers in python3 results in an
odd-looking highlight. Feel free to just take the text from my patch 😊



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-02 15:43                                     ` Stefan Kangas
@ 2025-02-03 14:54                                       ` kobarity
  2025-02-03 20:57                                         ` Stefan Kangas
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-02-03 14:54 UTC (permalink / raw)
  To: Konstantin Kharlamov, Stefan Kangas, Ship Mints,
	Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2199 bytes --]

Richard Stallman wrote:
> Python 2 and Python 3 seem rather different, and it seems that the
> same mode is being used for both.
>
> Would it be reasonable to make two slightly different modes for them,
> so as to highlight each language version as is best for that version?

Perhaps that is the way it should be.

Gabriel Santos wrote:
> The way the Python modes currently work is that there's a main python-base-mode,
> with python-mode and python-ts-mode deriving from it.
> So maybe we could have a python2-mode.

python-ts-mode is the tree-sitter version of python-mode.  As
tree-sitter supports both Python 2 and Python 3, there can be
python2-mode and python2-ts-mode.  Although I'm not sure if it's worth
preparing those now.

Stefan Kangas wrote:
> 
> kobarity <kobarity@gmail.com> writes:
> 
> > I'm attaching a patch as a reference for discussion.  This patch
> > introduces a new defcustom `python-2-support'.  You must save and
> > restart Emacs for it to work.  If `python-2-support' is non-nil,
> > Python 2 only builtins are highlighted in `python-ts-mode' as well as
> > `python-mode'.
> 
> Thanks, ideally we would not require restart but only `setopt`, but
> that's not a blocker.  It shouldn't be too hard to implement later, but
> would require functions that set up these variables.

Yes, but I'm concerned about people customizing
`python-font-lock-keywords-level-2' etc. in their init.el.  If
possible, I don't want to break their customizations.

> I couldn't see anything else that stood out in the patch in cursory
> reading, and I also didn't test it.  If you think it's good, I'd install
> it, and we can do further refinements later.

Thanks, I updated my patch.  However, I would like to think a little
more about this, including whether a new python2-mode is appropriate.

Konstantin Kharlamov wrote:
> It seems it would be nice to mention explicitly why the change is done,
> i.e. that highlighting python2-only identifiers in python3 results in an
> odd-looking highlight. Feel free to just take the text from my patch 😊

Thanks, I updated etc/NEWS.  I think it is better to distinguish
builtins from keywords.

[-- Attachment #2: 0001-Make-it-configurable-to-highlight-Python-2-builtins.patch --]
[-- Type: application/octet-stream, Size: 14105 bytes --]

From 9ede2b5e44ed33a8d8cc6cb306e534b6beadb893 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Mon, 3 Feb 2025 00:02:42 +0900
Subject: [PATCH] Make it configurable to highlight Python 2 builtins

By default, Python 2 only builtins are not highlighted.

* lisp/progmodes/python.el (python-2-support): New defcustom.
(python-font-lock-builtin-types)
(python-font-lock-builtins-python3)
(python-font-lock-builtins-python2)
(python-font-lock-builtins)
(python-font-lock-special-attributes)
(python-font-lock-builtin-exceptions-python3)
(python-font-lock-builtin-exceptions-python2)
(python-font-lock-builtin-exceptions): New variables.
(python-font-lock-keywords-level-2)
(python-font-lock-keywords-maximum-decoration)
(python--treesit-builtin-types)
(python--treesit-builtins)
(python--treesit-special-attributes)
(python--treesit-exceptions): Use new variables.

Co-authored-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
---
 etc/NEWS                 |  12 +++
 lisp/progmodes/python.el | 197 ++++++++++++++++++---------------------
 2 files changed, 103 insertions(+), 106 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8cf86d1375d..af51700552d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -980,6 +980,18 @@ exist.  If "python" points to Python 2 on your system, you now have to
 customize these variables to "python3" if you want to use Python 3
 instead.
 
+---
+*** New user option 'python-2-support'.
+By default, Python 2-only builtins such as "file" are not highlighted.
+Many of the Python 2-only builtins may appear as an identifier in Python
+3 code, and it's an odd user experience to see one being highlighted as
+a builtin.  Since Python 2 EOL has happened 5 years ago, this release
+removes Python 2-only builtins from the highlight to improve user
+experience of the modern Python users.  If you would like to highlight
+them, customize this option to non-nil and restart Emacs.  If this
+option is non-nil, Python 2-only builtins will be highlighted in
+'python-ts-mode' as well as 'python-mode'.
+
 ---
 *** Support of 'electric-layout-mode' added.
 
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 3a297a3979c..081b532ab04 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -316,6 +316,16 @@ python-interpreter-args
   :version "30.1"
   :type 'string)
 
+(defcustom python-2-support nil
+  "Whether to enable Python 2 support.
+If non-nil, enable Python 2 support.  Currently only affects
+highlighting.
+
+This variable only works after saving and restarting Emacs."
+  :version "31.1"
+  :type 'boolean
+  :safe 'booleanp)
+
 \f
 ;;; Bindings
 
@@ -689,6 +699,40 @@ python-font-lock-keywords-level-1
 This is the minimum decoration level, including function and
 class declarations.")
 
+(defvar python-font-lock-builtin-types
+  '("bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset"
+    "int" "list" "memoryview" "range" "set" "str" "tuple"))
+
+(defvar python-font-lock-builtins-python3
+  '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
+    "callable" "chr" "classmethod" "compile" "delattr" "dir" "divmod"
+    "enumerate" "eval" "exec" "filter" "format" "getattr" "globals"
+    "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
+    "issubclass" "iter" "len" "locals" "map" "max" "min" "next"
+    "object" "oct" "open" "ord" "pow" "print" "property" "repr"
+    "reversed" "round" "setattr" "slice" "sorted" "staticmethod" "sum"
+    "super" "type" "vars" "zip" "__import__"))
+
+(defvar python-font-lock-builtins-python2
+  '("basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
+    "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
+    "intern"))
+
+(defvar python-font-lock-builtins
+  (append python-font-lock-builtins-python3
+          (when python-2-support
+            python-font-lock-builtins-python2)))
+
+(defvar python-font-lock-special-attributes
+  '(;; https://docs.python.org/3/reference/datamodel.html
+    "__annotations__" "__bases__" "__closure__" "__code__"
+    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
+    "__globals__" "__kwdefaults__" "__name__" "__module__"
+    "__mro__" "__package__" "__qualname__"
+    "__static_attributes__" "__type_params__"
+    ;; Extras:
+    "__all__"))
+
 (defvar python-font-lock-keywords-level-2
   `(,@python-font-lock-keywords-level-1
     ,(rx symbol-start
@@ -711,33 +755,11 @@ python-font-lock-keywords-level-2
           "self")
          symbol-end)
     ;; Builtins
-    (,(rx symbol-start
-          (or
-           "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
-           "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
-           "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
-           "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
-           "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
-           "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
-           "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
-           "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
-           "__import__"
-           ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
-           "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
-           ;; Python 3:
-           "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
-           ;; Special attributes:
-           ;; https://docs.python.org/3/reference/datamodel.html
-           "__annotations__" "__bases__" "__closure__" "__code__"
-           "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-           "__globals__" "__kwdefaults__" "__name__" "__module__"
-           "__mro__" "__package__" "__qualname__"
-           "__static_attributes__" "__type_params__"
-           ;; Extras:
-           "__all__")
-          symbol-end) . font-lock-builtin-face))
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@(append python-font-lock-builtin-types
+                                        python-font-lock-builtins
+                                        python-font-lock-special-attributes))
+                          symbol-end)) . font-lock-builtin-face))
   "Font lock keywords to use in `python-mode' for level 2 decoration.
 
 This is the medium decoration level, including everything in
@@ -759,6 +781,41 @@ python-font-lock-assignment-matcher
                         (equal (char-after) ?=))
                return (progn (backward-char) t))))
 
+(defvar python-font-lock-builtin-exceptions-python3
+  '(;; Python 2 and 3:
+    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
+    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
+    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
+    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
+    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
+    "LookupError" "MemoryError" "NameError" "NotImplementedError"
+    "OSError" "OverflowError" "PendingDeprecationWarning"
+    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
+    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
+    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
+    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
+    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
+    "ZeroDivisionError"
+    ;; Python 3:
+    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
+    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
+    "ConnectionResetError" "EncodingWarning" "FileExistsError"
+    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
+    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
+    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
+    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
+    "BaseExceptionGroup" "ExceptionGroup"
+    ;; OS specific
+    "VMSError" "WindowsError"))
+
+(defvar python-font-lock-builtin-exceptions-python2
+  '("StandardError"))
+
+(defvar python-font-lock-builtin-exceptions
+  (append python-font-lock-builtin-exceptions-python3
+          (when python-2-support
+            python-font-lock-builtin-exceptions-python2)))
+
 (defvar python-font-lock-keywords-maximum-decoration
   `((python--font-lock-f-strings)
     ,@python-font-lock-keywords-level-2
@@ -776,38 +833,9 @@ python-font-lock-keywords-maximum-decoration
                                             (0+ "." (1+ (or word ?_)))))
      (1 font-lock-type-face))
     ;; Builtin Exceptions
-    (,(rx symbol-start
-          (or
-           ;; Python 2 and 3:
-           "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-           "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-           "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-           "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-           "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-           "LookupError" "MemoryError" "NameError" "NotImplementedError"
-           "OSError" "OverflowError" "PendingDeprecationWarning"
-           "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-           "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-           "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-           "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-           "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-           "ZeroDivisionError"
-           ;; Python 2:
-           "StandardError"
-           ;; Python 3:
-           "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-           "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-           "ConnectionResetError" "EncodingWarning" "FileExistsError"
-           "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-           "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-           "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-           "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-           "BaseExceptionGroup" "ExceptionGroup"
-           ;; OS specific
-           "VMSError" "WindowsError"
-           )
-          symbol-end)
-     . font-lock-type-face)
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@python-font-lock-builtin-exceptions)
+                          symbol-end)) . font-lock-type-face)
     ;; single assignment with/without type hints, e.g.
     ;;   a: int = 5
     ;;   b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo')
@@ -1015,8 +1043,7 @@ python--treesit-keywords
     "and" "in" "is" "not" "or" "not in" "is not"))
 
 (defvar python--treesit-builtin-types
-  '("int" "float" "complex" "bool" "list" "tuple" "range" "str"
-    "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
+  python-font-lock-builtin-types)
 
 (defvar python--treesit-type-regex
   (rx-to-string `(seq bol (or
@@ -1025,17 +1052,7 @@ python--treesit-type-regex
                   eol)))
 
 (defvar python--treesit-builtins
-  (append python--treesit-builtin-types
-          '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
-            "callable" "chr" "classmethod" "compile"
-            "delattr" "dir" "divmod" "enumerate" "eval" "exec"
-            "filter" "format" "getattr" "globals"
-            "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
-            "issubclass" "iter" "len" "locals" "map" "max"
-            "min" "next" "object" "oct" "open" "ord" "pow"
-            "print" "property" "repr" "reversed" "round"
-            "setattr" "slice" "sorted" "staticmethod" "sum" "super"
-            "type" "vars" "zip" "__import__")))
+  python-font-lock-builtins)
 
 (defvar python--treesit-constants
   '("Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
@@ -1047,42 +1064,10 @@ python--treesit-operators
     ">>" ">>=" "|" "|=" "~" "@" "@="))
 
 (defvar python--treesit-special-attributes
-  '("__annotations__" "__bases__" "__closure__" "__code__"
-    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-    "__globals__" "__kwdefaults__" "__name__" "__module__"
-    "__mro__" "__package__" "__qualname__"
-    "__static_attributes__" "__type_params__"
-    "__all__"))
+  python-font-lock-special-attributes)
 
 (defvar python--treesit-exceptions
-  '(;; Python 2 and 3:
-    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-    "LookupError" "MemoryError" "NameError" "NotImplementedError"
-    "OSError" "OverflowError" "PendingDeprecationWarning"
-    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-    "ZeroDivisionError"
-    ;; Python 2:
-    "StandardError"
-    ;; Python 3:
-    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-    "ConnectionResetError" "EncodingWarning" "FileExistsError"
-    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-    "BaseExceptionGroup" "ExceptionGroup"
-    ;; OS specific
-    "VMSError" "WindowsError"
-    ))
+  python-font-lock-builtin-exceptions)
 
 (defun python--treesit-fontify-string (node override start end &rest _)
   "Fontify string.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-03 14:54                                       ` kobarity
@ 2025-02-03 20:57                                         ` Stefan Kangas
  2025-02-04 11:45                                           ` Konstantin Kharlamov
  0 siblings, 1 reply; 35+ messages in thread
From: Stefan Kangas @ 2025-02-03 20:57 UTC (permalink / raw)
  To: kobarity, Konstantin Kharlamov, Ship Mints, Kévin Le Gouguec,
	emacs-devel

kobarity <kobarity@gmail.com> writes:

>> Thanks, ideally we would not require restart but only `setopt`, but
>> that's not a blocker.  It shouldn't be too hard to implement later, but
>> would require functions that set up these variables.
>
> Yes, but I'm concerned about people customizing
> `python-font-lock-keywords-level-2' etc. in their init.el.  If
> possible, I don't want to break their customizations.

Is that use case common?

>> I couldn't see anything else that stood out in the patch in cursory
>> reading, and I also didn't test it.  If you think it's good, I'd install
>> it, and we can do further refinements later.
>
> Thanks, I updated my patch.  However, I would like to think a little
> more about this, including whether a new python2-mode is appropriate.

Thanks, please do.  Maybe a separate mode is indeed easier to deal with,
given the complications that a variable brings.  I imagine that Python 2
files could then just say -*- mode: python2 -*- and be done with it (if
users on old versions add (defalias 'python2-mode 'python-mode).

Meanwhile, two additional small comments below.

> +*** New user option 'python-2-support'.
> +By default, Python 2-only builtins such as "file" are not highlighted.
> +Many of the Python 2-only builtins may appear as an identifier in Python
> +3 code, and it's an odd user experience to see one being highlighted as
> +a builtin.  Since Python 2 EOL has happened 5 years ago, this release
> +removes Python 2-only builtins from the highlight to improve user
> +experience of the modern Python users.  If you would like to highlight
> +them, customize this option to non-nil and restart Emacs.  If this
> +option is non-nil, Python 2-only builtins will be highlighted in
> +'python-ts-mode' as well as 'python-mode'.

The above might be a bit on the long side.  How about this:

*** Python 2 support is now optional and disabled by default.
Since Python 2 EOL was over 5 years ago, this release removes Python
2-only builtins such as "file" from the default highlighting in
'python-mode' and 'python-ts-mode'.  If you would like them highlighted,
customize the new user option `python-2-support' to a non-nil value and
restart Emacs.

> +(defcustom python-2-support nil
> +  "Whether to enable Python 2 support.
> +If non-nil, enable Python 2 support.  Currently only affects
> +highlighting.
> +
> +This variable only works after saving and restarting Emacs."

How about this:

  "If non-nil, enable Python 2 support.
Currently only affects highlighting.

After customizing this variable, you must restart Emacs for it to take
effect."



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-03 20:57                                         ` Stefan Kangas
@ 2025-02-04 11:45                                           ` Konstantin Kharlamov
  2025-02-04 12:29                                             ` tomas
  0 siblings, 1 reply; 35+ messages in thread
From: Konstantin Kharlamov @ 2025-02-04 11:45 UTC (permalink / raw)
  To: Stefan Kangas, kobarity, Ship Mints, Kévin Le Gouguec,
	emacs-devel

On Mon, 2025-02-03 at 20:57 +0000, Stefan Kangas wrote:
> kobarity <kobarity@gmail.com> writes:
> 
> > > Thanks, ideally we would not require restart but only `setopt`,
> > > but
> > > that's not a blocker.  It shouldn't be too hard to implement
> > > later, but
> > > would require functions that set up these variables.
> > 
> > Yes, but I'm concerned about people customizing
> > `python-font-lock-keywords-level-2' etc. in their init.el.  If
> > possible, I don't want to break their customizations.
> 
> Is that use case common?

You can never know, per Hyrum's law in combination with the lack of
statistics from users' installations.

> > > I couldn't see anything else that stood out in the patch in
> > > cursory
> > > reading, and I also didn't test it.  If you think it's good, I'd
> > > install
> > > it, and we can do further refinements later.
> > 
> > Thanks, I updated my patch.  However, I would like to think a
> > little
> > more about this, including whether a new python2-mode is
> > appropriate.
> 
> Thanks, please do.  Maybe a separate mode is indeed easier to deal
> with,
> given the complications that a variable brings.  I imagine that
> Python 2
> files could then just say -*- mode: python2 -*- and be done with it
> (if
> users on old versions add (defalias 'python2-mode 'python-mode).

I'm not clear where do you mean for `-*- mode: python2 -*-` to be
inserted in. If kobarity is okay with going the way of creating a
separate python2-mode, the easiest solution may be to just default to
python3-mode, and let users override it with `.dir-locals.el` or
whatnot. If it isn't too hard, we can also add the check for
`#.*\<python2\>` at the top of the file to decide which mode to enable.
I'm not sure anything else would be necessary.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-04 11:45                                           ` Konstantin Kharlamov
@ 2025-02-04 12:29                                             ` tomas
  2025-02-04 15:18                                               ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: tomas @ 2025-02-04 12:29 UTC (permalink / raw)
  To: Konstantin Kharlamov
  Cc: Stefan Kangas, kobarity, Ship Mints, Kévin Le Gouguec,
	emacs-devel

[-- Attachment #1: Type: text/plain, Size: 449 bytes --]

On Tue, Feb 04, 2025 at 02:45:58PM +0300, Konstantin Kharlamov wrote:
> On Mon, 2025-02-03 at 20:57 +0000, Stefan Kangas wrote:

[...]

> > Is that use case common?
> 
> You can never know, per Hyrum's law in combination with the lack of
> statistics from users' installations.

Besides, trying to not break uncommon users is something I do
appreciate Emacs for.

I understand there are limits: engineering and that.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-04 12:29                                             ` tomas
@ 2025-02-04 15:18                                               ` kobarity
  2025-02-04 22:28                                                 ` Stefan Kangas
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-02-04 15:18 UTC (permalink / raw)
  To: tomas, Konstantin Kharlamov, Stefan Kangas, Ship Mints,
	Kévin Le Gouguec, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 3459 bytes --]

Stefan Kangas wrote:
> 
> kobarity <kobarity@gmail.com> writes:
> 
> >> Thanks, ideally we would not require restart but only `setopt`, but
> >> that's not a blocker.  It shouldn't be too hard to implement later, but
> >> would require functions that set up these variables.
> >
> > Yes, but I'm concerned about people customizing
> > `python-font-lock-keywords-level-2' etc. in their init.el.  If
> > possible, I don't want to break their customizations.
> 
> Is that use case common?

I don't think so, but I want to maintain compatibility as much as
possible.

> > +*** New user option 'python-2-support'.
> > +By default, Python 2-only builtins such as "file" are not highlighted.
> > +Many of the Python 2-only builtins may appear as an identifier in Python
> > +3 code, and it's an odd user experience to see one being highlighted as
> > +a builtin.  Since Python 2 EOL has happened 5 years ago, this release
> > +removes Python 2-only builtins from the highlight to improve user
> > +experience of the modern Python users.  If you would like to highlight
> > +them, customize this option to non-nil and restart Emacs.  If this
> > +option is non-nil, Python 2-only builtins will be highlighted in
> > +'python-ts-mode' as well as 'python-mode'.
> 
> The above might be a bit on the long side.  How about this:
> 
> *** Python 2 support is now optional and disabled by default.
> Since Python 2 EOL was over 5 years ago, this release removes Python
> 2-only builtins such as "file" from the default highlighting in
> 'python-mode' and 'python-ts-mode'.  If you would like them highlighted,
> customize the new user option `python-2-support' to a non-nil value and
> restart Emacs.
> 
> > +(defcustom python-2-support nil
> > +  "Whether to enable Python 2 support.
> > +If non-nil, enable Python 2 support.  Currently only affects
> > +highlighting.
> > +
> > +This variable only works after saving and restarting Emacs."
> 
> How about this:
> 
>   "If non-nil, enable Python 2 support.
> Currently only affects highlighting.
> 
> After customizing this variable, you must restart Emacs for it to take
> effect."

Thanks, I updated the patch.

Konstantin Kharlamov wrote:
> 
> On Mon, 2025-02-03 at 20:57 +0000, Stefan Kangas wrote:
> > Thanks, please do.  Maybe a separate mode is indeed easier to deal
> > with,
> > given the complications that a variable brings.  I imagine that
> > Python 2
> > files could then just say -*- mode: python2 -*- and be done with it
> > (if
> > users on old versions add (defalias 'python2-mode 'python-mode).
> 
> I'm not clear where do you mean for `-*- mode: python2 -*-` to be
> inserted in. If kobarity is okay with going the way of creating a
> separate python2-mode, the easiest solution may be to just default to
> python3-mode, and let users override it with `.dir-locals.el` or
> whatnot. If it isn't too hard, we can also add the check for
> `#.*\<python2\>` at the top of the file to decide which mode to enable.
> I'm not sure anything else would be necessary.

That is one way to specify the mode in a file.  Please see:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Choosing-Modes.html

Emacs provides various means of specifying the mode.  This is the
primary reason I began to think that it might be a good idea to create
`python2-mode'.  Of course, the default mode should be `python-mode'
(for Python 3).

[-- Attachment #2: 0001-Make-it-configurable-to-highlight-Python-2-builtins.patch --]
[-- Type: application/octet-stream, Size: 13853 bytes --]

From 2682e96ad29b61b8079c2452034ed6732918573d Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Wed, 5 Feb 2025 00:02:05 +0900
Subject: [PATCH] Make it configurable to highlight Python 2 builtins

By default, Python 2-only builtins are not highlighted.

* lisp/progmodes/python.el (python-2-support): New defcustom.
(python-font-lock-builtin-types)
(python-font-lock-builtins-python3)
(python-font-lock-builtins-python2)
(python-font-lock-builtins)
(python-font-lock-special-attributes)
(python-font-lock-builtin-exceptions-python3)
(python-font-lock-builtin-exceptions-python2)
(python-font-lock-builtin-exceptions): New variables.
(python-font-lock-keywords-level-2)
(python-font-lock-keywords-maximum-decoration)
(python--treesit-builtin-types)
(python--treesit-builtins)
(python--treesit-special-attributes)
(python--treesit-exceptions): Use new variables.

Co-authored-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Co-authored-by: Stefan Kangas <stefankangas@gmail.com>
---
 etc/NEWS                 |   8 ++
 lisp/progmodes/python.el | 197 ++++++++++++++++++---------------------
 2 files changed, 99 insertions(+), 106 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8cf86d1375d..6138a4e010a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -980,6 +980,14 @@ exist.  If "python" points to Python 2 on your system, you now have to
 customize these variables to "python3" if you want to use Python 3
 instead.
 
+---
+*** Python 2 support is now optional and disabled by default.
+Since Python 2 EOL was over 5 years ago, this release removes Python
+2-only builtins such as "file" from the default highlighting in
+'python-mode' and 'python-ts-mode'.  If you would like them highlighted,
+customize the new user option `python-2-support' to a non-nil value and
+restart Emacs.
+
 ---
 *** Support of 'electric-layout-mode' added.
 
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 3a297a3979c..8a99ff0434d 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -316,6 +316,16 @@ python-interpreter-args
   :version "30.1"
   :type 'string)
 
+(defcustom python-2-support nil
+  "If non-nil, enable Python 2 support.
+Currently only affects highlighting.
+
+After customizing this variable, you must restart Emacs for it to take
+effect."
+  :version "31.1"
+  :type 'boolean
+  :safe 'booleanp)
+
 \f
 ;;; Bindings
 
@@ -689,6 +699,40 @@ python-font-lock-keywords-level-1
 This is the minimum decoration level, including function and
 class declarations.")
 
+(defvar python-font-lock-builtin-types
+  '("bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset"
+    "int" "list" "memoryview" "range" "set" "str" "tuple"))
+
+(defvar python-font-lock-builtins-python3
+  '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
+    "callable" "chr" "classmethod" "compile" "delattr" "dir" "divmod"
+    "enumerate" "eval" "exec" "filter" "format" "getattr" "globals"
+    "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
+    "issubclass" "iter" "len" "locals" "map" "max" "min" "next"
+    "object" "oct" "open" "ord" "pow" "print" "property" "repr"
+    "reversed" "round" "setattr" "slice" "sorted" "staticmethod" "sum"
+    "super" "type" "vars" "zip" "__import__"))
+
+(defvar python-font-lock-builtins-python2
+  '("basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
+    "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
+    "intern"))
+
+(defvar python-font-lock-builtins
+  (append python-font-lock-builtins-python3
+          (when python-2-support
+            python-font-lock-builtins-python2)))
+
+(defvar python-font-lock-special-attributes
+  '(;; https://docs.python.org/3/reference/datamodel.html
+    "__annotations__" "__bases__" "__closure__" "__code__"
+    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
+    "__globals__" "__kwdefaults__" "__name__" "__module__"
+    "__mro__" "__package__" "__qualname__"
+    "__static_attributes__" "__type_params__"
+    ;; Extras:
+    "__all__"))
+
 (defvar python-font-lock-keywords-level-2
   `(,@python-font-lock-keywords-level-1
     ,(rx symbol-start
@@ -711,33 +755,11 @@ python-font-lock-keywords-level-2
           "self")
          symbol-end)
     ;; Builtins
-    (,(rx symbol-start
-          (or
-           "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
-           "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
-           "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
-           "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
-           "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
-           "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
-           "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
-           "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
-           "__import__"
-           ;; Python 2:
-           "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
-           "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
-           "intern"
-           ;; Python 3:
-           "aiter" "anext" "ascii" "breakpoint" "bytearray" "bytes" "exec"
-           ;; Special attributes:
-           ;; https://docs.python.org/3/reference/datamodel.html
-           "__annotations__" "__bases__" "__closure__" "__code__"
-           "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-           "__globals__" "__kwdefaults__" "__name__" "__module__"
-           "__mro__" "__package__" "__qualname__"
-           "__static_attributes__" "__type_params__"
-           ;; Extras:
-           "__all__")
-          symbol-end) . font-lock-builtin-face))
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@(append python-font-lock-builtin-types
+                                        python-font-lock-builtins
+                                        python-font-lock-special-attributes))
+                          symbol-end)) . font-lock-builtin-face))
   "Font lock keywords to use in `python-mode' for level 2 decoration.
 
 This is the medium decoration level, including everything in
@@ -759,6 +781,41 @@ python-font-lock-assignment-matcher
                         (equal (char-after) ?=))
                return (progn (backward-char) t))))
 
+(defvar python-font-lock-builtin-exceptions-python3
+  '(;; Python 2 and 3:
+    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
+    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
+    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
+    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
+    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
+    "LookupError" "MemoryError" "NameError" "NotImplementedError"
+    "OSError" "OverflowError" "PendingDeprecationWarning"
+    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
+    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
+    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
+    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
+    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
+    "ZeroDivisionError"
+    ;; Python 3:
+    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
+    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
+    "ConnectionResetError" "EncodingWarning" "FileExistsError"
+    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
+    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
+    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
+    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
+    "BaseExceptionGroup" "ExceptionGroup"
+    ;; OS specific
+    "VMSError" "WindowsError"))
+
+(defvar python-font-lock-builtin-exceptions-python2
+  '("StandardError"))
+
+(defvar python-font-lock-builtin-exceptions
+  (append python-font-lock-builtin-exceptions-python3
+          (when python-2-support
+            python-font-lock-builtin-exceptions-python2)))
+
 (defvar python-font-lock-keywords-maximum-decoration
   `((python--font-lock-f-strings)
     ,@python-font-lock-keywords-level-2
@@ -776,38 +833,9 @@ python-font-lock-keywords-maximum-decoration
                                             (0+ "." (1+ (or word ?_)))))
      (1 font-lock-type-face))
     ;; Builtin Exceptions
-    (,(rx symbol-start
-          (or
-           ;; Python 2 and 3:
-           "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-           "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-           "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-           "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-           "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-           "LookupError" "MemoryError" "NameError" "NotImplementedError"
-           "OSError" "OverflowError" "PendingDeprecationWarning"
-           "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-           "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-           "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-           "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-           "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-           "ZeroDivisionError"
-           ;; Python 2:
-           "StandardError"
-           ;; Python 3:
-           "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-           "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-           "ConnectionResetError" "EncodingWarning" "FileExistsError"
-           "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-           "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-           "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-           "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-           "BaseExceptionGroup" "ExceptionGroup"
-           ;; OS specific
-           "VMSError" "WindowsError"
-           )
-          symbol-end)
-     . font-lock-type-face)
+    (,(rx-to-string `(seq symbol-start
+                          (or ,@python-font-lock-builtin-exceptions)
+                          symbol-end)) . font-lock-type-face)
     ;; single assignment with/without type hints, e.g.
     ;;   a: int = 5
     ;;   b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo')
@@ -1015,8 +1043,7 @@ python--treesit-keywords
     "and" "in" "is" "not" "or" "not in" "is not"))
 
 (defvar python--treesit-builtin-types
-  '("int" "float" "complex" "bool" "list" "tuple" "range" "str"
-    "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
+  python-font-lock-builtin-types)
 
 (defvar python--treesit-type-regex
   (rx-to-string `(seq bol (or
@@ -1025,17 +1052,7 @@ python--treesit-type-regex
                   eol)))
 
 (defvar python--treesit-builtins
-  (append python--treesit-builtin-types
-          '("abs" "aiter" "all" "anext" "any" "ascii" "bin" "breakpoint"
-            "callable" "chr" "classmethod" "compile"
-            "delattr" "dir" "divmod" "enumerate" "eval" "exec"
-            "filter" "format" "getattr" "globals"
-            "hasattr" "hash" "help" "hex" "id" "input" "isinstance"
-            "issubclass" "iter" "len" "locals" "map" "max"
-            "min" "next" "object" "oct" "open" "ord" "pow"
-            "print" "property" "repr" "reversed" "round"
-            "setattr" "slice" "sorted" "staticmethod" "sum" "super"
-            "type" "vars" "zip" "__import__")))
+  python-font-lock-builtins)
 
 (defvar python--treesit-constants
   '("Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
@@ -1047,42 +1064,10 @@ python--treesit-operators
     ">>" ">>=" "|" "|=" "~" "@" "@="))
 
 (defvar python--treesit-special-attributes
-  '("__annotations__" "__bases__" "__closure__" "__code__"
-    "__defaults__" "__dict__" "__doc__" "__firstlineno__"
-    "__globals__" "__kwdefaults__" "__name__" "__module__"
-    "__mro__" "__package__" "__qualname__"
-    "__static_attributes__" "__type_params__"
-    "__all__"))
+  python-font-lock-special-attributes)
 
 (defvar python--treesit-exceptions
-  '(;; Python 2 and 3:
-    "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
-    "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
-    "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
-    "GeneratorExit" "IOError" "ImportError" "ImportWarning"
-    "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
-    "LookupError" "MemoryError" "NameError" "NotImplementedError"
-    "OSError" "OverflowError" "PendingDeprecationWarning"
-    "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
-    "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
-    "TypeError" "UnboundLocalError" "UnicodeDecodeError"
-    "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
-    "UnicodeWarning" "UserWarning" "ValueError" "Warning"
-    "ZeroDivisionError"
-    ;; Python 2:
-    "StandardError"
-    ;; Python 3:
-    "BlockingIOError" "BrokenPipeError" "ChildProcessError"
-    "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
-    "ConnectionResetError" "EncodingWarning" "FileExistsError"
-    "FileNotFoundError" "InterruptedError" "IsADirectoryError"
-    "NotADirectoryError" "ModuleNotFoundError" "PermissionError"
-    "ProcessLookupError" "PythonFinalizationError" "RecursionError"
-    "ResourceWarning" "StopAsyncIteration" "TimeoutError"
-    "BaseExceptionGroup" "ExceptionGroup"
-    ;; OS specific
-    "VMSError" "WindowsError"
-    ))
+  python-font-lock-builtin-exceptions)
 
 (defun python--treesit-fontify-string (node override start end &rest _)
   "Fontify string.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-04 15:18                                               ` kobarity
@ 2025-02-04 22:28                                                 ` Stefan Kangas
  2025-02-05 15:20                                                   ` kobarity
  0 siblings, 1 reply; 35+ messages in thread
From: Stefan Kangas @ 2025-02-04 22:28 UTC (permalink / raw)
  To: kobarity, tomas, Konstantin Kharlamov, Ship Mints,
	Kévin Le Gouguec, emacs-devel

kobarity <kobarity@gmail.com> writes:

> From 2682e96ad29b61b8079c2452034ed6732918573d Mon Sep 17 00:00:00 2001
> From: kobarity <kobarity@gmail.com>
> Date: Wed, 5 Feb 2025 00:02:05 +0900
> Subject: [PATCH] Make it configurable to highlight Python 2 builtins
>
> By default, Python 2-only builtins are not highlighted.

Thanks, pushed to master as commit 999d054dc1e.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-04 22:28                                                 ` Stefan Kangas
@ 2025-02-05 15:20                                                   ` kobarity
  2025-02-05 23:25                                                     ` Stefan Kangas
  0 siblings, 1 reply; 35+ messages in thread
From: kobarity @ 2025-02-05 15:20 UTC (permalink / raw)
  To: Stefan Kangas
  Cc: tomas, Konstantin Kharlamov, Ship Mints, Kévin Le Gouguec,
	emacs-devel

Stefan Kangas wrote:
> 
> kobarity <kobarity@gmail.com> writes:
> 
> > From 2682e96ad29b61b8079c2452034ed6732918573d Mon Sep 17 00:00:00 2001
> > From: kobarity <kobarity@gmail.com>
> > Date: Wed, 5 Feb 2025 00:02:05 +0900
> > Subject: [PATCH] Make it configurable to highlight Python 2 builtins
> >
> > By default, Python 2-only builtins are not highlighted.
> 
> Thanks, pushed to master as commit 999d054dc1e.

Thanks, I will continue to consider implementing `python2-mode'.



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3] Stop highlighting Python2 keywords.
  2025-02-05 15:20                                                   ` kobarity
@ 2025-02-05 23:25                                                     ` Stefan Kangas
  0 siblings, 0 replies; 35+ messages in thread
From: Stefan Kangas @ 2025-02-05 23:25 UTC (permalink / raw)
  To: kobarity
  Cc: tomas, Konstantin Kharlamov, Ship Mints, Kévin Le Gouguec,
	emacs-devel

kobarity <kobarity@gmail.com> writes:

> Stefan Kangas wrote:
>>
>> kobarity <kobarity@gmail.com> writes:
>>
>> > From 2682e96ad29b61b8079c2452034ed6732918573d Mon Sep 17 00:00:00 2001
>> > From: kobarity <kobarity@gmail.com>
>> > Date: Wed, 5 Feb 2025 00:02:05 +0900
>> > Subject: [PATCH] Make it configurable to highlight Python 2 builtins
>> >
>> > By default, Python 2-only builtins are not highlighted.
>>
>> Thanks, pushed to master as commit 999d054dc1e.
>
> Thanks, I will continue to consider implementing `python2-mode'.

That will be appreciated.  Thank you.



^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2025-02-05 23:25 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-22 12:57 [RFE] python-mode: removing `file` keyword from syntax highlight Konstantin Kharlamov
2025-01-24  7:50 ` Konstantin Kharlamov
2025-01-24 16:03   ` kobarity
2025-01-24 17:08     ` Stefan Kangas
2025-01-24 17:28     ` Konstantin Kharlamov
2025-01-24 19:14       ` Howard Melman
2025-01-25  8:25         ` Konstantin Kharlamov
2025-01-25 11:10       ` Kévin Le Gouguec
2025-01-25 11:23         ` Ship Mints
2025-01-25 15:08           ` kobarity
2025-01-25 20:54             ` Konstantin Kharlamov
2025-01-27 14:27               ` kobarity
2025-01-29  5:48                 ` [PATCH] Remove Python2 "file" from the list of keywords Konstantin Kharlamov
2025-01-29 14:26                   ` kobarity
2025-01-29 22:19                     ` [PATCH v2] " Konstantin Kharlamov
2025-01-30  1:56                       ` Stefan Kangas
2025-01-30  7:07                         ` Konstantin Kharlamov
2025-01-30 15:15                           ` kobarity
2025-01-30 19:00                             ` [PATCH v3] Stop highlighting Python2 keywords Konstantin Kharlamov
2025-01-31  0:45                               ` Stefan Kangas
2025-01-31 14:53                                 ` kobarity
2025-02-02 15:21                                   ` kobarity
2025-02-02 15:43                                     ` Stefan Kangas
2025-02-03 14:54                                       ` kobarity
2025-02-03 20:57                                         ` Stefan Kangas
2025-02-04 11:45                                           ` Konstantin Kharlamov
2025-02-04 12:29                                             ` tomas
2025-02-04 15:18                                               ` kobarity
2025-02-04 22:28                                                 ` Stefan Kangas
2025-02-05 15:20                                                   ` kobarity
2025-02-05 23:25                                                     ` Stefan Kangas
2025-02-02 22:54                                     ` Konstantin Kharlamov
2025-02-01 20:09                                 ` Richard Stallman
2025-02-01 21:17                                   ` Gabriel Santos
2025-02-02 15:35                                   ` Stefan Kangas

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.