-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
stub for csv.writer can't accept subclass of csv.Dialect #3611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for finding! It's confusing to me that there is csv.Dialect and _csv.Dialect in the first place. @JelleZijlstra suggested this was for performance reasons. Given that all the CPython code I can see seems to duck type around dialects, maybe the right thing to do is to just re-export _csv.Dialect in csv. Seems like |
Resolves python#3611 Tested with mypy on: ``` f = open("asdf.csv", "w") csv.writer(f) csv.writer(f, dialect=csv.excel) csv.writer(f, dialect=csv.excel()) csv.DictReader(f) csv.DictReader(f, dialect=csv.excel) csv.DictReader(f, dialect=csv.excel()) class CustomDialect(csv.Dialect): delimiter = "%" csv.writer(f, dialect=CustomDialect) csv.writer(f, dialect=CustomDialect()) ```
Resolves python#3611 Tested with mypy on: ``` f = open("asdf.csv", "w") csv.writer(f) csv.writer(f, dialect=csv.excel) csv.writer(f, dialect=csv.excel()) csv.DictReader(f) csv.DictReader(f, dialect=csv.excel) csv.DictReader(f, dialect=csv.excel()) class CustomDialect(csv.Dialect): delimiter = "%" csv.writer(f, dialect=CustomDialect) csv.writer(f, dialect=CustomDialect()) ```
Resolves #3611 Tested with mypy on: ``` f = open("asdf.csv", "w") csv.writer(f) csv.writer(f, dialect=csv.excel) csv.writer(f, dialect=csv.excel()) csv.DictReader(f) csv.DictReader(f, dialect=csv.excel) csv.DictReader(f, dialect=csv.excel()) class CustomDialect(csv.Dialect): delimiter = "%" csv.writer(f, dialect=CustomDialect) csv.writer(f, dialect=CustomDialect()) ```
csv.writer
is an import of_csv.writer
. #3581 changed_csv.writer
to takedialect: Union[_csv.Dialect, str]
. This is correct, but nowcsv.Dialect
-- which is distinct from_csv.Dialect
-- is no longer a valid argument type forcsv.writer
.For example, if someone was to define their own
class MyDialect(csv.Dialect)
, a type checker would be correct to raise a "wrong argument type" error for a call likecsv.writer(some_file, dialect=MyDialect())
.As far as I can tell, the stub is technically correct, but
_csv.writer
should be able to take subclasses ofcsv.writer
. (Or at least I assume that's the intention. Notably,_csv.Dialect
has astrict
attribute thatcsv.Dialect
lacks.)The text was updated successfully, but these errors were encountered: