Always implement proper validation and type checking to prevent cryptic error messages. When errors do occur, provide clear, actionable guidance for resolution.
Key practices:
# Prefer this
if isinstance(path, io.BytesIO):
target = path
# Over potentially error-prone checks that could lead to attribute errors
# Validate inputs early with helpful error messages
if len(exprs) == 1 and isinstance(exprs[0], Mapping):
msg = (
"Cannot pass a dictionary as a single positional argument.\n"
"If you merely want the *keys*, use:\n"
" • df.method(*your_dict.keys())\n"
"If you need the key–value pairs, use one of:\n"
" • unpack as keywords: df.method(**your_dict)\n"
" • build expressions: df.method(expr.alias(k) for k, expr in your_dict.items())"
)
raise TypeError(msg)
Consider returning None instead of raising exceptions for non-critical failures to allow more consistent error handling by callers.
Enter the URL of a public GitHub repository