GNU bug report logs - #63410
30.0.50; [FR] Optionally allow defcustom to check type for standard value

Previous Next

Package: emacs;

Reported by: Ruijie Yu <ruijie <at> netyu.xyz>

Date: Wed, 10 May 2023 05:37:01 UTC

Severity: wishlist

Found in version 30.0.50

To reply to this bug, email your comments to 63410 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Wed, 10 May 2023 05:37:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ruijie Yu <ruijie <at> netyu.xyz>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 10 May 2023 05:37:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ruijie Yu <ruijie <at> netyu.xyz>
To: bug-gnu-emacs <at> gnu.org
Subject: 30.0.50; [FR] Optionally allow defcustom to check type for standard
 value
Date: Wed, 10 May 2023 13:22:34 +0800
Similar to `setopt' introduced in 29, which warns when a variable gets
assigned a value with an incorrect type, I hope `defcustom' can
(optionally) check that the standard value of a customizable variable is
correct.  This would help external as well as internal package authors
to catch errors on the types.

I don't know how to define such optionality though -- can/should it also
be a customizable variable?  Or maybe it should just be a
function/command which checks customizable variables (e.g., a single
variable; a group; all of them; etc.), and warns about all the type
mismatches along the way?

In addition, a "check all variables within this group (and
subgroups)" might be helpful as well.

At the moment, the only via workaround I know of to check existing value
type for any given variable is this:

    (setopt variable variable)

But this introduces the runtime cost of the :set function, which
sometimes do pretty expensive things like (re-) loading a package.

Another workaround I know of, of course, is to run

    M-x customize-variable

on the variable, but that is painfully slow compared to the first
workaround.

If no-one gets to it first, I may start looking into how to implement it
within a week or two.

-- 
Best,


RY




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Wed, 10 May 2023 13:37:01 GMT) Full text and rfc822 format available.

Message #8 received at 63410 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Ruijie Yu <ruijie <at> netyu.xyz>, "63410 <at> debbugs.gnu.org"
 <63410 <at> debbugs.gnu.org>
Subject: RE: [External] : bug#63410: 30.0.50; [FR] Optionally allow defcustom
 to check type for standard value
Date: Wed, 10 May 2023 13:36:49 +0000
> Similar to `setopt' introduced in 29, which warns when a variable gets
> assigned a value with an incorrect type, I hope `defcustom' can
> (optionally) check that the standard value of a customizable variable is
> correct.  This would help external as well as internal package authors
> to catch errors on the types.

Not sure I understand.  Doesn't it do that already,
showing "mismatch" next to the default value if it
doesn't match the type definition?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Wed, 10 May 2023 14:52:02 GMT) Full text and rfc822 format available.

Message #11 received at 63410 <at> debbugs.gnu.org (full text, mbox):

From: Ruijie Yu <ruijie <at> netyu.xyz>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: "63410 <at> debbugs.gnu.org" <63410 <at> debbugs.gnu.org>
Subject: Re: bug#63410: 30.0.50; [FR] Optionally allow defcustom to check
 type for standard value
Date: Wed, 10 May 2023 22:51:14 +0800
Drew Adams <drew.adams <at> oracle.com> writes:

>> Similar to `setopt' introduced in 29, which warns when a variable gets
>> assigned a value with an incorrect type, I hope `defcustom' can
>> (optionally) check that the standard value of a customizable variable is
>> correct.  This would help external as well as internal package authors
>> to catch errors on the types.
>
> Not sure I understand.  Doesn't it do that already,
> showing "mismatch" next to the default value if it
> doesn't match the type definition?

Yes, it does, but I think it is not "automated" enough, since you would
have to go over the variables one at a time.

My request is that Emacs should (optionally, maybe behind a defcustom,
or configure, or cli flag, etc.) warn you warn a defcustom's standard
value does not match its declared type.  Since a similar warning is
already in place for `setopt', I don't think it is farfetched to request
`defcustom' to follow suit.

Alternatively, or in addition to the above, maybe provide a
function/command that checks types for a group+subgroups, or for all
defined customizable variables.  Again, this would help package authors
to catch typing errors which would otherwise be quite difficult to
catch.

As I mentioned upthread, using `setopt' directly like the following
snippet is unnecessarily expensive: it calls the :set function of a
customizable variable, where the only thing we want is to check its
type.

```emacs-lisp
(defcustom expensive-var t
  :type 'string
  :set (lambda (n v)
         (dotimes (_ 424242) 'expensive)
         (set n v)))
(setopt expensive-var expensive-var)
;; warns the user that the type is correct, but only after spending a
;; long time in the :set function
```

-----

Let me also explain the reason behind this request.  I have encountered
this scenario multiple times, where when I `setopt' something for a
customizable variable according to its docs, I get a warning saying that
the type is incorrect.  Examples of this scenario include eshell, mu4e,
and pyim.

Seeing this type of warning would prompt me to examine the entire
package in which the warning occurs.  I would have to rgrep the entire
package, and examine the types of each defcustom one-by-one, fixing all
type errors along the way.  When I encounter difficult-to-parse types I
would do the `(setopt var var)' method to save time and brain cells.

And here comes my feature request.  We have the warning in `setopt'; we
have some visual indication in the customization view; and I want to
have one more: to know when a defcustom call defines a wrongly-typed
standard value.

-- 
Best,


RY




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Wed, 10 May 2023 15:40:01 GMT) Full text and rfc822 format available.

Message #14 received at 63410 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ruijie Yu <ruijie <at> netyu.xyz>
Cc: 63410 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#63410: 30.0.50;
 [FR] Optionally allow defcustom to check type for standard value
Date: Wed, 10 May 2023 18:40:19 +0300
> Cc: "63410 <at> debbugs.gnu.org" <63410 <at> debbugs.gnu.org>
> Date: Wed, 10 May 2023 22:51:14 +0800
> From:  Ruijie Yu via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
> 
> My request is that Emacs should (optionally, maybe behind a defcustom,
> or configure, or cli flag, etc.) warn you warn a defcustom's standard
> value does not match its declared type.

Warn you at what time?  When defcustom is byte-compiled? when it is
evaluated at run time? when the user says "M-x set-variable"?
something else?

> Since a similar warning is already in place for `setopt', I don't
> think it is farfetched to request `defcustom' to follow suit.

setopt says it when you invoke it.  But setopt is designed to be
called as a function at run time, whereas defcustom isn't.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Thu, 11 May 2023 02:27:01 GMT) Full text and rfc822 format available.

Message #17 received at 63410 <at> debbugs.gnu.org (full text, mbox):

From: Ruijie Yu <ruijie <at> netyu.xyz>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 63410 <at> debbugs.gnu.org, drew.adams <at> oracle.com
Subject: Re: bug#63410: 30.0.50; [FR] Optionally allow defcustom to check
 type for standard value
Date: Thu, 11 May 2023 10:25:41 +0800
Eli Zaretskii <eliz <at> gnu.org> writes:

>> Cc: "63410 <at> debbugs.gnu.org" <63410 <at> debbugs.gnu.org>
>> Date: Wed, 10 May 2023 22:51:14 +0800
>> From:  Ruijie Yu via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
>> 
>> My request is that Emacs should (optionally, maybe behind a defcustom,
>> or configure, or cli flag, etc.) warn you warn a defcustom's standard
>> value does not match its declared type.
>
> Warn you at what time?  When defcustom is byte-compiled? when it is
> evaluated at run time? when the user says "M-x set-variable"?
> something else?

I was initially thinking of at runtime, but now that you mention it,
byte-compilation time sounds more reasonable.

So, I would say check the type unconditionally on byte-compilation time,
or when something is toggled on, check the type at runtime if a
defcustom is not compiled.

>> Since a similar warning is already in place for `setopt', I don't
>> think it is farfetched to request `defcustom' to follow suit.
>
> setopt says it when you invoke it.  But setopt is designed to be
> called as a function at run time, whereas defcustom isn't.

In that case, I had my semi-parallel second proposal: provide a command
that checks for a group of variables and see if types of all variables
within the group (or its subgroups) are correct.

-- 
Best,


RY




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63410; Package emacs. (Thu, 11 May 2023 14:09:02 GMT) Full text and rfc822 format available.

Message #20 received at 63410 <at> debbugs.gnu.org (full text, mbox):

From: Ruijie Yu <ruijie <at> netyu.xyz>
To: 63410 <at> debbugs.gnu.org
Cc: Eli Zaretskii <eliz <at> gnu.org>, drew.adams <at> oracle.com
Subject: Re: bug#63410: 30.0.50; [FR] Optionally allow defcustom to check
 type for standard value
Date: Thu, 11 May 2023 22:08:20 +0800
[Message part 1 (text/plain, inline)]
Ruijie Yu via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org> writes:

> In that case, I had my semi-parallel second proposal: provide a command
> that checks for a group of variables and see if types of all variables
> within the group (or its subgroups) are correct.

Regarding the second proposal: I have quickly hacked together a
functional example of type checking, see the attachment.  If we think
this should be integrated into Emacs, I can make some minor edits and
propose a patch.  In that case, I would probably put the functions
somewhere in cus-edit.el or custom.el.

Note that at the moment it depends on the internal function
`custom--standard-value', which is a one-line function accessing a
property of the symbol.

[cfg-check-defcustom.el (text/plain, attachment)]
[Message part 3 (text/plain, inline)]
Also, note that I added the symbol into the warning text because the
goal of this command is to mass-check an entire group, whereas `setopt'
did not mention the variable symbol name.  Not sure if `setopt' should
show the symbol in trouble, but I understand if we don't want to do
that.

-- 
Best,


RY

Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefankangas <at> gmail.com> to control <at> debbugs.gnu.org. (Wed, 06 Sep 2023 20:29:02 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 283 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.