{"record":{"id":"33d3de5e4b13b346","repo":"pandas-dev/pandas","slug":"cannot-override-builtin-dialect","errorCode":null,"errorMessage":"Cannot override builtin dialect.","messagePattern":"Cannot override builtin dialect\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/_testing/contexts.py","lineNumber":122,"sourceCode":"    name : str\n        The name of the dialect.\n    kwargs : mapping\n        The parameters for the dialect.\n\n    Raises\n    ------\n    ValueError : the name of the dialect conflicts with a builtin one.\n\n    See Also\n    --------\n    csv : Python's CSV library.\n    \"\"\"\n    import csv\n\n    _BUILTIN_DIALECTS = {\"excel\", \"excel-tab\", \"unix\"}\n\n    if name in _BUILTIN_DIALECTS:\n        raise ValueError(\"Cannot override builtin dialect.\")\n\n    csv.register_dialect(name, **kwargs)\n    try:\n        yield\n    finally:\n        csv.unregister_dialect(name)\n\n\ndef raises_chained_assignment_error(\n    extra_warnings: tuple[type[Warning], ...] = (),\n    extra_match: tuple[str | None, ...] = (),\n) -> AbstractContextManager:\n    from pandas._testing import assert_produces_warning\n\n    if CHAINED_WARNING_DISABLED:\n        if not extra_warnings:\n            from contextlib import nullcontext\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/_testing/contexts.py#L104-L140","documentation":"Raised by the csv_dialect context manager (contexts.py:96-128, a pandas._testing helper) when you try to register a CSV dialect whose name collides with one of Python's builtin dialects: 'excel', 'excel-tab', or 'unix'. The guard (contexts.py:119-122) protects csv.register_dialect from shadowing builtins, since overriding them can corrupt all subsequent CSV parsing.","triggerScenarios":"Entering `with tm.csv_dialect('excel', **kwargs):` or tm.csv_dialect('excel-tab', ...) or tm.csv_dialect('unix', ...). The name is checked against _BUILTIN_DIALECTS before csv.register_dialect is called.","commonSituations":"Writing a CSV-parsing test and picking a short, 'obvious' dialect name that happens to be a builtin; generic test-fixture code that auto-names dialects and occasionally lands on 'excel'.","solutions":["Choose a non-builtin dialect name, e.g. 'my_excel', 'test_dialect', 'pandas_test'.","Check the name against {'excel', 'excel-tab', 'unix'} before passing it in.","If you genuinely need excel-like behavior, use the builtin 'excel' dialect directly without registering a new one."],"exampleFix":"# before\nwith tm.csv_dialect('excel', delimiter=';'):\n    ...\n\n# after\nwith tm.csv_dialect('my_excel', delimiter=';'):\n    ...","handlingStrategy":"validation","validationCode":"BUILTIN = {'excel', 'excel-tab', 'unix'}\nname = get_dialect_name()\nif name in BUILTIN:\n    raise ValueError(f'{name} is a builtin CSV dialect')\nwith tm.csv_dialect(name, **kwargs):\n    ...","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a project-specific dialect name prefix (e.g. 'test_') to avoid collisions.","Check the name against the builtin set before registering.","Reuse the builtin 'excel' dialect directly when you only need Excel-like parsing."],"tags":["testing","csv","context-manager","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}