[Python] Disallow passing f-strings as argument

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am reporting data from some tests, and each test can have a 'summary' and a 'details' section. The 'summary' field for any particular test should be static, with any additional dynamic information going into the details field, as follows:

    run_test()
    if condition:
    report_test_data(summary="condition met", details=f"{component} ended with {state}")
    else:
    report_test_data(summary="condition not met", details=f{component} ended with {state}")


    However, this only applies to these calls to report_test_data, and there is nothing to stop another test from swapping these around, or putting all the data into the 'summary' field:

    run_test()
    if condition:
    report_test_data(summary=f"{component} ended with {state} - condition met", details="")
    else:
    report_test_data(summary=f"{component} ended with {state} - condition not met", details="")


    I am analyzing the test data based off the summary, so any particular ending state (e.g. condition = True) should have a static return string. I thought about making a class that manually defines every possible 'summary' string, but that quickly becomes untenable with more tests when a given test can have tens of possible ending states. The best option I can think of is if I could force the value passed into 'summary' to be a normal string. Is there any way to disallow passing f-strings into a particular function?

    Note: I use pylint, so if there's an easy way to make it call these out, that would work as well.

    Continue reading...

Compartilhe esta Página