Skip to content
DevMeme
4560 of 7590
CodeQuality Post #5002 · source on Telegram

Python truthiness helper mixing strings and booleans illustrates naïve input validation humor

Description

Screenshot of a dark-themed code editor showing two lines of Python with syntax highlighting. Line 1 reads: "def is_truthy(s):" in which the keyword "def" is aqua, the function name "is_truthy" light blue, and parameters white. Line 2 is indented and reads: "return s in ['True', 'true', 'Yes', 'yes', True]" with "return" in yellow, brackets in white, the four string literals in pink, and the bare Boolean literal True in bold cyan. The snippet attempts to decide whether a value should be considered truthy by checking membership in a heterogeneous list that mixes strings and a Boolean, creating an obvious type-comparison pitfall and ignoring other common cases like "1" or different casing. Seasoned developers will recognize this as a code smell demonstrating poor input validation, hidden bugs from type coercion, and the classic need for clearer normalization logic or a mapping table

Comments

32
Anonymous ★ Top Pick Our “robust” truthiness check: a list that treats 'yes', 'True', and, thanks to Python’s bool-int equality, the integer 1 as the same flag. Nothing like shipping a feature where page views accidentally enable production mode
  1. Anonymous ★ Top Pick

    Our “robust” truthiness check: a list that treats 'yes', 'True', and, thanks to Python’s bool-int equality, the integer 1 as the same flag. Nothing like shipping a feature where page views accidentally enable production mode

  2. Anonymous

    After 20 years in tech, I've learned that 'boolean' is just a suggestion - it's really a string enum with infinite possible values, each client having their own creative interpretation of what constitutes 'true'

  3. Anonymous

    Ah yes, the classic 'is_truthy' function that returns True when you pass it the string 'True' but False when you pass it the string 'False' - because apparently we're still debugging that YAML parser from 2015 where everything became a string. This is what happens when your API accepts form data, JSON, and carrier pigeons all through the same endpoint, and you've given up on type hints because 'Python is dynamically typed anyway.' Bonus points: this will fail spectacularly when someone inevitably passes 'TRUE', '1', or 'y', leading to a 3 AM incident where the root cause is 'case sensitivity in boolean string parsing' - a phrase that should never exist in production code but somehow always does

  4. Anonymous

    Accepts 'True', 'Yes', and - thanks to bool==int - the number 1; enjoy your retries=1 query param enabling the production feature flag

  5. Anonymous

    def is_truthy(s): return s in ['True','true','Yes','yes', True] - when you try to parse booleans and accidentally ship YAML 1.1 semantics; bonus: 1, 1.0, and np.bool_ also count as “yes” because bool is int

  6. Anonymous

    Because strict typing is for interviews; prod configs demand diplomatic truth parsing

  7. @koloslolya 3y

    What about "y", " Y" and "why not?" ?

  8. @korlivan 3y

    What about 1?

    1. @choke_hazard 3y

      What about sqrt(1) and 1^n?

      1. @HellAbyss 3y

        What about ln(e) and -cos(pi)?

        1. @NevermindExpress 3y

          What about f(x)=1?

      2. @unknown24907 3y

        =1

      3. @gizlu 3y

        if(eval(s) in [1,....

        1. @callofvoid0 3y

          eval is dangerous

          1. @gizlu 3y

            No shit

          2. @SamsonovAnton 3y

            eval is evil.

            1. @ZgGPuo8dZef58K6hxxGVj3Z2 3y

              Nah man you are wrong just use it properly: eval(“(()=>{ usrNm = \”” + document.getElementById(“usernamebox”).Text + “\”;})();”);

  9. @choke_hazard 3y

    I'm pretty sure we also need a byte array/hash of this image

  10. @unknown24907 3y

    where are 1,2,…

  11. @unknown24907 3y

    it is not haskell

    1. @callofvoid0 3y

      python

      1. @unknown24907 3y

        there is no laziness

        1. @callofvoid0 3y

          and by this term what are we tryna result in?

  12. @SamsonovAnton 3y

    Hey, what about everything that is not zero/null?

    1. @M4lenov 3y

      Maybe "No" is not truthy

      1. @SamsonovAnton 3y

        You say that to char No[] = "No"; printf("%s\n", (No ? "Yes!" : "Oh no!")); 😝

  13. @realVitShadyTV 3y

    What about stop using this shitty lang for dumb nerds?

  14. @ZgGPuo8dZef58K6hxxGVj3Z2 3y

    What about sqrt(a*a+a*a)?

  15. @affirvega 3y

    yaml be like

  16. 𝙳𝚖𝚢𝚝𝚛𝚘 𝙼𝚒𝚗𝚝𝚎𝚗𝚔𝚘 3y

    it says perhaps in Ukrainian

    1. @pavloalpha 3y

      Perhaps...

  17. @kaitodev 3y

    What about !0 ?

Use J and K for navigation