Check if a string value needs to be quoted. Quoting is needed when: - Value contains special characters (comma, colon, newline, quotes) - Value has leading or trailing whitespace - Value looks like a boolean or null literal - Value is empty Args: value:
(value: str)
| 9 | |
| 10 | |
| 11 | def needs_quoting(value: str) -> bool: |
| 12 | """ |
| 13 | Check if a string value needs to be quoted. |
| 14 | |
| 15 | Quoting is needed when: |
| 16 | - Value contains special characters (comma, colon, newline, quotes) |
| 17 | - Value has leading or trailing whitespace |
| 18 | - Value looks like a boolean or null literal |
| 19 | - Value is empty |
| 20 | |
| 21 | Args: |
| 22 | value: String to check |
| 23 | |
| 24 | Returns: |
| 25 | True if quoting is needed, False otherwise |
| 26 | """ |
| 27 | if not value: |
| 28 | return True |
| 29 | |
| 30 | # Check for leading/trailing whitespace |
| 31 | if value != value.strip(): |
| 32 | return True |
| 33 | |
| 34 | # Check if it looks like a literal |
| 35 | lower_value = value.lower() |
| 36 | if lower_value in (TRUE_LITERAL, FALSE_LITERAL, NULL_LITERAL): |
| 37 | return True |
| 38 | |
| 39 | # Check for special characters |
| 40 | special_chars = {COMMA, COLON, NEWLINE, QUOTE, TAB, PIPE, BACKSLASH, '[', ']', '{', '}'} |
| 41 | if any(char in value for char in special_chars): |
| 42 | return True |
| 43 | |
| 44 | # Check if it looks like a number but has trailing content |
| 45 | # This handles cases like "123abc" which should be quoted |
| 46 | try: |
| 47 | float(value) |
| 48 | return False |
| 49 | except ValueError: |
| 50 | pass |
| 51 | |
| 52 | return False |
| 53 | |
| 54 | |
| 55 | def escape_string(value: str) -> str: |
no outgoing calls
no test coverage detected
searching dependent graphs…