Skip to content

Replace format string to f-strings in python file#990

Open
kenya-sk wants to merge 14 commits intotensorflow:masterfrom
kenya-sk:replace-to-f-strings
Open

Replace format string to f-strings in python file#990
kenya-sk wants to merge 14 commits intotensorflow:masterfrom
kenya-sk:replace-to-f-strings

Conversation

@kenya-sk
Copy link
Contributor

@kenya-sk kenya-sk commented Feb 8, 2026

The Python program's strings were written in format strings, so I wrote them using the f-strings recommended in recent Python versions.
No functional changes were made during this rewrite. It was limited to syntactic changes.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@kenya-sk kenya-sk marked this pull request as draft February 8, 2026 04:54
@kenya-sk kenya-sk marked this pull request as ready for review February 23, 2026 13:42
@mhucka
Copy link
Member

mhucka commented Feb 25, 2026

@kenya-sk Thank you for doing this. This PR seems to mix (a) converting format strings to be f-strings and (b) other format changes from Yapf. This PR's title and description only reflect the changes in (a). Per discussion on issue #983, it would be better to split up the two kinds of changes. So, could you pull out the non-f-string changes to another PR?

@kenya-sk
Copy link
Contributor Author

@mhucka
Thank you for your review.
The reason the two tasks are mixed in this PR is that I integrated the pre-merged branch changed to avoid CI errors.
I believe merging the PR #991 first should resolve this issue. Could you please take a look and confirm.

If it would be better to keep this PRs separated even before merging, I'm happy to adjust accordingly.
Please let me know.

@mhucka
Copy link
Member

mhucka commented Feb 25, 2026

Ah, I see. In that case, I'll work with you on getting #991 submitted. I have a question on that one (asked in the comments on that PR).

@kenya-sk kenya-sk marked this pull request as draft February 25, 2026 03:46
@kenya-sk kenya-sk marked this pull request as ready for review February 27, 2026 01:16
@gemini-code-assist
Copy link

Important

Installation incomplete: to start using Gemini Code Assist, please ask the organization owner(s) to visit the Gemini Code Assist Admin Console and sign the Terms of Services.

@kenya-sk
Copy link
Contributor Author

Hi @mhucka,

I pulled the latest master branch, which includes the PyLint fixes (PR #991).
This PR now only contains the f-strings changes.

Please review it when you have time.

@MichaelHudgins
Copy link

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces .format() string formatting with f-strings across multiple Python files, which is a good modernization effort. The changes are mostly correct and improve readability. I've made a few suggestions to further improve the readability of some of the new f-strings, particularly where they are split across multiple lines or constructed from multiple concatenated f-strings.

Comment on lines +50 to +51
SRC, "reports/", f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")

Choose a reason for hiding this comment

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

medium

For readability, you can combine these two concatenated f-strings into a single one. This makes the code cleaner and easier to understand.

Suggested change
SRC, "reports/", f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")
SRC, "reports/",
f"CliffordBenchmarks.benchmark_clifford_circuit_{params.n_qubits}_{params.n_moments}_{params.batch_size}")

Comment on lines +59 to +60
res.name, f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")

Choose a reason for hiding this comment

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

medium

Similar to the previous comment, combining these concatenated f-strings into a single f-string will improve code clarity.

Suggested change
res.name, f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")
res.name,
f"CliffordBenchmarks.benchmark_clifford_circuit_{params.n_qubits}_{params.n_moments}_{params.batch_size}")

Comment on lines +56 to +57
f"RandomCircuitBenchmarks.benchmark_random_circuit_"
f"{params.n_rows}_{params.n_cols}_{params.n_moments}")

Choose a reason for hiding this comment

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

medium

To improve readability, it's better to use a single f-string here instead of concatenating two.

            f"RandomCircuitBenchmarks.benchmark_random_circuit_{params.n_rows}_{params.n_cols}_{params.n_moments}")

Comment on lines +65 to +66
res.name, f"RandomCircuitBenchmarks.benchmark_random_circuit_"
f"{params.n_rows}_{params.n_cols}_{params.n_moments}")

Choose a reason for hiding this comment

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

medium

For better code clarity, you can combine these two f-strings into one.

            res.name,
            f"RandomCircuitBenchmarks.benchmark_random_circuit_{params.n_rows}_{params.n_cols}_{params.n_moments}")

Comment on lines 324 to +326
raise TypeError('num_samples tensor must have the same shape '
'as pauli_sums tensor. got: {} expected: {}'.format(
num_samples.shape, pauli_sums.shape))
f'as pauli_sums tensor. got: {num_samples.shape}'
f' expected: {pauli_sums.shape}')

Choose a reason for hiding this comment

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

medium

This string construction is a mix of a regular string and two f-strings. For better readability and consistency, it would be cleaner to use a single f-string enclosed in parentheses to handle the line break.

Suggested change
raise TypeError('num_samples tensor must have the same shape '
'as pauli_sums tensor. got: {} expected: {}'.format(
num_samples.shape, pauli_sums.shape))
f'as pauli_sums tensor. got: {num_samples.shape}'
f' expected: {pauli_sums.shape}')
raise TypeError((
'num_samples tensor must have the same shape '
f'as pauli_sums tensor. got: {num_samples.shape} '
f'expected: {pauli_sums.shape}'))

Comment on lines 127 to 132
raise ValueError('unexpected signature for analytic_op. '
'Given arg: {}.'.format(str(key)) + ''
'The signature should contain: {}.'.format(
list(expected_signature)) + ''
' Given: {}'.format(list(signature)) + ''
f'Given arg: {key}.' + ''
'The signature should contain: '
f'{list(expected_signature)}' + ''
f' Given: {list(signature)}' + ''
'Note: noisy ops should use sampled_op')

Choose a reason for hiding this comment

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

medium

The string concatenation with '' is unnecessary. You can combine these into a single f-string for better readability.

Suggested change
raise ValueError('unexpected signature for analytic_op. '
'Given arg: {}.'.format(str(key)) + ''
'The signature should contain: {}.'.format(
list(expected_signature)) + ''
' Given: {}'.format(list(signature)) + ''
f'Given arg: {key}.' + ''
'The signature should contain: '
f'{list(expected_signature)}' + ''
f' Given: {list(signature)}' + ''
'Note: noisy ops should use sampled_op')
raise ValueError(
f'unexpected signature for analytic_op. '
f'Given arg: {key}. '
'The signature should contain: '
f'{list(expected_signature)} '
f' Given: {list(signature)} '
'Note: noisy ops should use sampled_op')

Comment on lines 148 to +151
raise ValueError('unexpected signature for sampled_op. '
'Given arg: {}.'.format(str(key)) + ''
'The signature should contain: {}.'.format(
list(expected_signature)))
f'Given arg: {key}.' + ''
'The signature should contain: '
f'{list(expected_signature)}')

Choose a reason for hiding this comment

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

medium

The string concatenation with '' can be removed by combining these into a single f-string, which improves readability.

Suggested change
raise ValueError('unexpected signature for sampled_op. '
'Given arg: {}.'.format(str(key)) + ''
'The signature should contain: {}.'.format(
list(expected_signature)))
f'Given arg: {key}.' + ''
'The signature should contain: '
f'{list(expected_signature)}')
raise ValueError(
f'unexpected signature for sampled_op. '
f'Given arg: {key}. '
'The signature should contain: '
f'{list(expected_signature)}')

Comment on lines 472 to 477
raise ValueError(
"Attempted to scan for symbols in circuit with unsupported"
" ops inside.", "Expected op found in "
"tfq.util.get_supported_gates but found: {}.".format(str(op)),
"Attempted to scan for symbols in circuit with unsupported "
"ops inside. Expected op found in "
f"tfq.util.get_supported_gates but found: {op}. "
"Please make sure circuits contain only ops found in "
"tfq.util.get_supported_gates().")

Choose a reason for hiding this comment

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

medium

This ValueError message is constructed from multiple string literals. It can be simplified into a single f-string for better readability.

Suggested change
raise ValueError(
"Attempted to scan for symbols in circuit with unsupported"
" ops inside.", "Expected op found in "
"tfq.util.get_supported_gates but found: {}.".format(str(op)),
"Attempted to scan for symbols in circuit with unsupported "
"ops inside. Expected op found in "
f"tfq.util.get_supported_gates but found: {op}. "
"Please make sure circuits contain only ops found in "
"tfq.util.get_supported_gates().")
raise ValueError(
f"Attempted to scan for symbols in circuit with unsupported "
f"ops inside. Expected op found in "
f"tfq.util.get_supported_gates but found: {op}. "
"Please make sure circuits contain only ops found in "
"tfq.util.get_supported_gates().")

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.

3 participants