From 717fb3563facff330f074cb01e58b6ec21bf493d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 2 Jan 2018 11:00:46 +0100 Subject: [PATCH 1/2] Add StringIO.name and BytesIO.name Also, change the type of StringIO.name (Python 3) from str to Any. Neither StringIO nor BytesIO actually define a name field, but the super-class IO[T] of both in typeshed does define a read-only property. This means that sub-classes of StringIO and BytesIO adding this field will not typecheck correctly. Closes: #1790 --- stdlib/2/_io.pyi | 8 ++++++++ stdlib/3/io.pyi | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 3f1baab15792..951c280b6a82 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -79,6 +79,10 @@ class BytesIO(_BufferedIOBase): def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... + # BytesIO does not contain a "name" field. This workaround is necessary + # to allow BytesIO sub-classes to add this field, as it is defined + # as a read-only property on IO[]. + name: Any = ... def getvalue(self) -> bytes: ... def write(self, s: bytes) -> int: ... def writelines(self, lines: Iterable[bytes]) -> None: ... @@ -145,6 +149,10 @@ class StringIO(_TextIOBase): newline: Optional[unicode] = ...) -> None: ... def __setstate__(self, state: tuple) -> None: ... def __getstate__(self) -> tuple: ... + # StringIO does not contain a "name" field. This workaround is necessary + # to allow StringIO sub-classes to add this field, as it is defined + # as a read-only property on IO[]. + name: Any = ... def getvalue(self) -> unicode: ... class TextIOWrapper(_TextIOBase): diff --git a/stdlib/3/io.pyi b/stdlib/3/io.pyi index ea2752dd73f4..a509afae349c 100644 --- a/stdlib/3/io.pyi +++ b/stdlib/3/io.pyi @@ -95,6 +95,10 @@ class FileIO(RawIOBase): # TODO should extend from BufferedIOBase class BytesIO(BinaryIO): def __init__(self, initial_bytes: bytes = ...) -> None: ... + # BytesIO does not contain a "name" field. This workaround is necessary + # to allow BytesIO sub-classes to add this field, as it is defined + # as a read-only property on IO[]. + name: Any = ... def getvalue(self) -> bytes: ... if sys.version_info >= (3, 2): def getbuffer(self) -> memoryview: ... @@ -248,7 +252,10 @@ class TextIOWrapper(TextIO): class StringIO(TextIOWrapper): def __init__(self, initial_value: str = ..., newline: Optional[str] = ...) -> None: ... - name = ... # type: str + # StringIO does not contain a "name" field. This workaround is necessary + # to allow StringIO sub-classes to add this field, as it is defined + # as a read-only property on IO[]. + name: Any = ... def getvalue(self) -> str: ... def __enter__(self) -> 'StringIO': ... From 90e32e67fe9a8e2bd5633d333597284fd8faf377 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 2 Jan 2018 13:33:57 +0100 Subject: [PATCH 2/2] StringIO/BytesIO.name: Remove unnecessary default values --- stdlib/2/_io.pyi | 4 ++-- stdlib/3/io.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 951c280b6a82..e2b66d25acba 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -82,7 +82,7 @@ class BytesIO(_BufferedIOBase): # BytesIO does not contain a "name" field. This workaround is necessary # to allow BytesIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. - name: Any = ... + name: Any def getvalue(self) -> bytes: ... def write(self, s: bytes) -> int: ... def writelines(self, lines: Iterable[bytes]) -> None: ... @@ -152,7 +152,7 @@ class StringIO(_TextIOBase): # StringIO does not contain a "name" field. This workaround is necessary # to allow StringIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. - name: Any = ... + name: Any def getvalue(self) -> unicode: ... class TextIOWrapper(_TextIOBase): diff --git a/stdlib/3/io.pyi b/stdlib/3/io.pyi index a509afae349c..88efad2885da 100644 --- a/stdlib/3/io.pyi +++ b/stdlib/3/io.pyi @@ -98,7 +98,7 @@ class BytesIO(BinaryIO): # BytesIO does not contain a "name" field. This workaround is necessary # to allow BytesIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. - name: Any = ... + name: Any def getvalue(self) -> bytes: ... if sys.version_info >= (3, 2): def getbuffer(self) -> memoryview: ... @@ -255,7 +255,7 @@ class StringIO(TextIOWrapper): # StringIO does not contain a "name" field. This workaround is necessary # to allow StringIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. - name: Any = ... + name: Any def getvalue(self) -> str: ... def __enter__(self) -> 'StringIO': ...