Skip to content

fix: avoiding usage of _ if already defined #3423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2021

Conversation

henryiii
Copy link
Collaborator

@henryiii henryiii commented Oct 28, 2021

Description

Closes #3401 by avoiding using a raw _.

Suggested changelog entry:

* Replace ``_`` with ``const_name`` in internals, avoid defining ``pybind::_`` if ``_`` defined as macro (common gettext usage).

@henryiii henryiii force-pushed the henryiii/fix/_ branch 3 times, most recently from abb94d5 to 9b374b0 Compare October 28, 2021 21:48
@jagerman
Copy link
Member

This is a majorly backwards incompatible change because it breaks documented practices for custom type casters since pretty much forever (see https://pybind11.readthedocs.io/en/stable/advanced/cast/custom.html, and the fact that you had to change the custom casters in test code to get it to compile).

@henryiii
Copy link
Collaborator Author

henryiii commented Oct 29, 2021

Didn't realize this was so public, but was mostly playing around to see what it would take. The _("") is really non-descriptive (and does, as the issue states, interfere with a very common practice for doing translations). We could provide a backward compatible definition of _ that could would be disabled if a _ macro was defined.

Also, am I correct in my assessment that it's really just for compile time manipulatable strings?

Found some useful docs improvements that I'll probably pull out.

@henryiii henryiii marked this pull request as draft October 29, 2021 02:20
@henryiii henryiii changed the title fix: avoid usage of _ DO NOT MERGE: experiment with avoiding usage of _ Oct 29, 2021
@henryiii
Copy link
Collaborator Author

I might try the "_" only if no macro exists idea.

@henryiii
Copy link
Collaborator Author

This renames _ to const_str, and also defines _, but only if not already defined as a macro. I can move a few of the tests back to using _ so we cover both. We can also move the docs to recommending the new spelling, or just keep it as an internal detail for cases where there is a conflict. Personally, I think it reads better instead of _(), which is usually means it supports translations. Thoughts?

@jagerman
Copy link
Member

Also, am I correct in my assessment that it's really just for compile time manipulatable strings?

It's been a while, but yes, as I recall, that is the main purpose -- by being compile-time constants they can avoid getting compiled into the actual final code. I think @wjakob probably chose _ because it was syntactically minimal.

Replacing it for 3.0 definitely seems good to me; in the meantime, perhaps it could go behind an opt-in macro?

@henryiii
Copy link
Collaborator Author

The current system seems pretty safe and backward compatible; both const_str and _ are available, internally only const_str is used, and if _ is defined as a macro, then _ is not available, just const_str. Maybe we could then drop it in 3.0, but it's pretty minimal (just four lines or so), so not that hard to maintain.

Basically it's an opt-out macro, and the macro is exactly the thing it collides with anyway.

@henryiii henryiii changed the title DO NOT MERGE: experiment with avoiding usage of _ WIP: experiment with avoiding usage of _ Oct 29, 2021
@henryiii henryiii marked this pull request as ready for review November 8, 2021 21:35
@henryiii henryiii changed the title WIP: experiment with avoiding usage of _ fix: avoiding usage of _ if already defined Nov 8, 2021
@jagerman
Copy link
Member

jagerman commented Dec 1, 2021

This needs a doc update, too: the _ is still used in the examples in https://pybind11.readthedocs.io/en/stable/advanced/cast/custom.html

@henryiii
Copy link
Collaborator Author

henryiii commented Dec 1, 2021

Do we want to change the docs over? I think it's more readable, so I'd be in favor. We can leave _ available indefinitely due to its previous prevalence.

@rwgk
Copy link
Collaborator

rwgk commented Dec 1, 2021

Do we want to change the docs over? I think it's more readable, so I'd be in favor. We can leave _ available indefinitely due to its previous prevalence.

I'm strongly in favor if changing the docs to const_str. I'm really glad to see this change, the _ is so easy to overlook, so difficult to pin-point, and gives no clue to what it's for.

@henryiii henryiii closed this Dec 14, 2021
@henryiii henryiii reopened this Dec 14, 2021
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Randomly crossed my mind:

const_char_array would be a more truthful / expressive name.

py::str is something completely different from py::detail::const_str.

@jagerman
Copy link
Member

jagerman commented Dec 15, 2021

Randomly crossed my mind:

const_char_array would be a more truthful / expressive name.

Kind of, but the point of this function isn't to be a const char array, it's meant only to be a compile-time static name for a class, and being a const char array just happens to be the current implementation detail. So perhaps "py::type_name" or something along that line would be a better choice?

@henryiii
Copy link
Collaborator Author

I think the overlap between py::detail::const_str and py::str is fine - there can't be a const version of py::str, since it's a Python object, and you really don't see the py::detail part, it's always just const_str in the type caster. It's important to keep it fairly clear and short. _ is a bit too much, but const_char_array would be too long. (And, eventually, it might be able to use a constexpr string, perhaps?)

const_name or static_name or static_str are ideas, though.

I can add a test. I'm wondering if adding a _ define is tripping up Boost.

@rwgk
Copy link
Collaborator

rwgk commented Dec 15, 2021

const_name or static_name or static_str are ideas, though.

const_name sounds good to me, in line with Jason's idea to emphasize high level vs impl detail.

static has so many meanings already ... thinking, better not.

@henryiii
Copy link
Collaborator Author

So const_str, const_name, and type_name (which I missed from before).

@rwgk
Copy link
Collaborator

rwgk commented Dec 17, 2021 via email

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

I just took another look. Perfect, thanks a lot for doing this!

@henryiii
Copy link
Collaborator Author

Can I get one more approval? @jagerman, @Skylion007, or someone else?

Copy link
Member

@jagerman jagerman left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@henryiii henryiii merged commit 39fbc79 into pybind:master Dec 21, 2021
@henryiii henryiii deleted the henryiii/fix/_ branch December 21, 2021 19:24
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Dec 21, 2021
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Dec 21, 2021
rwgk added a commit to rwgk/pybind11 that referenced this pull request Dec 22, 2021
rwgk added a commit to rwgk/pybind11 that referenced this pull request Dec 28, 2021
Also exercises pybind11::detail::_ backward compatibility.

See also: PR pybind#3423
rwgk added a commit that referenced this pull request Dec 29, 2021
* Adding dedicated test_const_name.

Also exercises pybind11::detail::_ backward compatibility.

See also: PR #3423

* Backing out tests involving int_to_str (requires C++17 or higher).

* Suppressing clang-tidy errors.

* Disabling test_const_name for MSVC 2015 due to bizarre failures.

* Stacking @pytest.mark.parametrize (thanks to @Skylion007 for pointing out).
rwgk added a commit that referenced this pull request Dec 29, 2021
copybara-service bot pushed a commit to pybind/pybind11_protobuf that referenced this pull request Oct 19, 2023
…1_protobuf.

This is a functional no-op.

Note that `_` was deprecated already in Dec 2021 (pybind/pybind11#3423).

PiperOrigin-RevId: 574885200
copybara-service bot pushed a commit to pybind/pybind11_protobuf that referenced this pull request Oct 20, 2023
…1_protobuf.

This is a functional no-op.

Note that `_` was deprecated already in Dec 2021 (pybind/pybind11#3423).

PiperOrigin-RevId: 574885200
copybara-service bot pushed a commit to pybind/pybind11_protobuf that referenced this pull request Oct 20, 2023
…1_protobuf.

This is a functional no-op.

Note that `_` was deprecated already in Dec 2021 (pybind/pybind11#3423).

PiperOrigin-RevId: 575085924
copybara-service bot pushed a commit to pybind/pybind11_abseil that referenced this pull request Oct 20, 2023
…1_protobuf.

This is a functional no-op.

Note that `_` was deprecated already in Dec 2021 (pybind/pybind11#3423).

PiperOrigin-RevId: 575085924
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: Underscore function conflicts with gettext common usage
4 participants