Skip to content
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

STYLE: Prefer using f-strings #198

Merged
merged 1 commit into from Dec 7, 2023
Merged

Conversation

jhlegarreta
Copy link
Contributor

Prefer using f-strings.

Prefer using f-strings.
@jhlegarreta jhlegarreta marked this pull request as ready for review November 30, 2023 19:42
Copy link
Contributor

@pieper pieper left a comment

Choose a reason for hiding this comment

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

@jhlegarreta it would be nice to think about preparing SlicerDMRI for internationalization. I don't think we have a developer guide for this yet, but one aspect is that we are using .format instead of f-strings (e.g. like this). The reason is that if we used f-strings then translations of that string could introduce arbitrary code which would be a security issue.

Although I like f-strings generally, I think we should try to make the code ready for i18n.

@jhlegarreta
Copy link
Contributor Author

The reason is that if we used f-strings then translations of that string could introduce arbitrary code which would be a security issue.

Do you know whether the Python folks are investigating this pitfall on f-strings while being used for i18n?

If they are, that would be excellent, and would give more value to this PR.

Otherwise, TBH, it would be a very low priority task for me to use the .format() string formatting model: the toolkit has other more fundamental issues or needs IMO.

On a slightly related note, the CI builds logs are full of traces related to internationalization, e.g.
https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/7051428476/job/19194285977?pr=198#step:6:26037

Seem to be warnings related to 3D Slicer or CTK, stemming from some Qt deprecation. It may be worthwhile to address those to avoid a visual pollution that prevents from identifying other warnings.

And this happens despite telling CMake not to build 3D Slicer with i18n support:
https://github.com/SlicerDMRI/SlicerDMRI/blob/master/.github/workflows/build-test.yml#L65

@pieper
Copy link
Contributor

pieper commented Nov 30, 2023

Do you know whether the Python folks are investigating this pitfall on f-strings while being used for i18n?

I have no idea. Probably the correct thing is to parse the f-string and only present the actual text for translation.

On a slightly related note, the CI builds logs are full of traces related to internationalization, e.g. https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/7051428476/job/19194285977?pr=198#step:6:26037

Can you point to a specific example or two? This log is huge and barely loads. The internationalization support is still new, so it's not a surprise that there are still some issues to resolve.

@jhlegarreta
Copy link
Contributor Author

Can you point to a specific example or two? This log is huge and barely loads. The internationalization support is still new, so it's not a surprise that there are still some issues to resolve.

If you allow sufficient time, the line posted will lead you directly to one of the messaged related to i18n.

@pieper
Copy link
Contributor

pieper commented Nov 30, 2023

@lassoan @mhdiop do have ideas about the warnings that @jhlegarreta pointed to?

@lassoan
Copy link
Contributor

lassoan commented Dec 1, 2023

We don't do anything special for i18n in Slicer, just use translation functions in a very simple, straightforward way. The build warnings are probably due to PythonQt and you can safely ignore it. PythonQt developers don't aim for warning-free code on all compilers. I would prefer if they tried a bit harder but it's an open-source library, so I guess anybody who is bothered could submit a pull request to fix warnings. You may try to use the latest Qt5 version, maybe it helps.

Python's recommendation is to use named placeholders in translatable text. Python's core translation infrastructure (gettext) does not support f-strings at all. Our infrastructure could take f-strings, but it would be unsafe.

Note that messages for developers must not be translated (it would make debugging much harder and unnecessarily increase translation workload), so you can keep using any string functions for messages that are only shown to developers. It could make sense to use print function for messages that are intended for end users and logging functions for messages for developers.

@jhlegarreta
Copy link
Contributor Author

We don't do anything special for i18n in Slicer, just use translation functions in a very simple, straightforward way. The build warnings are probably due to PythonQt and you can safely ignore it. PythonQt developers don't aim for warning-free code on all compilers. I would prefer if they tried a bit harder but it's an open-source library, so I guess anybody who is bothered could submit a pull request to fix warnings. You may try to use the latest Qt5 version, maybe it helps.

Yes, the warnings look harmless, but they are polluting the logs and the builds in important ways, and prevent to navigate through them and to easily locate potentially harmful warnings.

Even more so when we have instructed CMake not use i18n at all:

          -DSlicer_BUILD_I18N_SUPPORT:BOOL=OFF \

https://github.com/SlicerDMRI/SlicerDMRI/blob/master/.github/workflows/build-test.yml#L65

Note that messages for developers must not be translated (it would make debugging much harder and unnecessarily increase translation workload), so you can keep using any string functions for messages that are only shown to developers. It could make sense to use print function for messages that are intended for end users and logging functions for messages for developers.

I agree; a proper logging system should be in place. However, this is a low priority now. And of course, I do not aim at translating any logged message; i18n is even a low priority for the toolkit in its current state.

@jhlegarreta
Copy link
Contributor Author

The warning comes from Qt 5.15.2:
https://github.com/CipSoft-Tibia/Qt5/blob/40f5aa5ff6f981ecd2403c70dd2419dfd9ae4d0b/qtbase/src/corelib/text/qlocale.h#L941

Qt 5.12.11 still has it:
https://github.com/qt/qtbase/blob/5.15/src/corelib/text/qlocale.h#L941

The first release removing the warning is 6.0.0. Does 3D Slicer support Qt 6.0.x @lassoan @jcfr ?

@lassoan
Copy link
Contributor

lassoan commented Dec 1, 2023

Yes, the warnings look harmless, but they are polluting the logs and the builds in important ways, and prevent to navigate through them and to easily locate potentially harmful warnings.

If you do a complete build: it helps if you review all the warnings once; and then only have look at the delta (new warnings). The dashboard shows new warnings separately (https://slicer.cdash.org/viewBuildError.php?type=1&onlydeltap&buildid=3224528), so it can be used.

During development: you usually modify one or few files at a time, so seeing the extra warnings is annoying but should be manageable (at least it does not cause problems for me on Windows). If there are so many warnings that you cannot even see them easily even when modifying a single file then it may worth the effort of reducing them.

The first release removing the warning is 6.0.0. Does 3D Slicer support Qt 6.0.x @lassoan @jcfr ?

Qt6 must be supported first by PythonQt and CTK. Probably it will happen sometime in 2024.

@jhlegarreta
Copy link
Contributor Author

Well aware of all the above; the issue is inspecting the CI builds.

Qt6 must be MeVisLab/pythonqt#13 and commontk/CTK#947. Probably it will happen sometime in 2024.

OK. Thanks.

@ljod ljod merged commit 56b282f into SlicerDMRI:master Dec 7, 2023
1 check failed
@jhlegarreta jhlegarreta deleted the UsefStrings branch December 7, 2023 16:04
@jhlegarreta
Copy link
Contributor Author

Qt6 must be supported first by PythonQt and CTK. Probably it will happen sometime in 2024.

@lassoan Looks like there starts to be some alpha releases for Qt6 in PythonQt:
MeVisLab/pythonqt#13 (comment)
https://github.com/MeVisLab/pythonqt/tags

@lassoan
Copy link
Contributor

lassoan commented Dec 22, 2023

Yes, thanks for the ping, it seems that @jamesobutler has already started looking into it.

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.

None yet

4 participants