pythonpylintf-string

Disallow passing f-strings as argument


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.


Solution

  • As commented by @Tzane , I ended up using enums. It was a pain to define an enumerator for each specific test, but this way I was able to require all test reporting results to be of type enum , which forces the strings to be static each time.

    To be honest, my main takeaway is that the main test results should definitely not be reported as a string, because it requires more work to parse and analyze later.