Skip to content

Should it be possible to subclass TextIO without implementing all abstract methods? #112

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

Closed
gvanrossum opened this issue May 18, 2015 · 3 comments
Labels
resolution: out of scope The idea or discussion was out of scope for this repository topic: feature Discussions about new features for Python's type annotations

Comments

@gvanrossum
Copy link
Member

Currently this fails:

from typing import TextIO
class MyTextFile(TextIO):
    pass
f = MyTextFile()  # raises TypeError

The instantiation fails with TypeError: Can't instantiate abstract class MyTextFile with abstract methods <long list of methods>.

Mark Shannon pointed this out. I have to think about whether this is important to support; a simple work-around is to use TextIO.register() instead:

from typing import TextIO
class MyTextFile:
    pass
TextIO.register(MyTextFile)
f = MyTextFile()  # OK
@NYKevin
Copy link

NYKevin commented May 28, 2015

Some (disjointed) thoughts:

  • Does Mypy support register()?
  • If we want to allow people to create subclasses without implementing <long list of methods>, why are they included in the abstract class? Could we make some of them mix-in methods instead?
  • If we want to call any of <long list of methods>, and we have an instance of TextIO, should we be prepared to handle an AttributeError?
  • I like the way the I/O system handles seekability in this regard (i.e. there's a seekable() method which returns True or False) -- could other features provide a similar API?
  • What if we broke TextIO apart into a bunch of smaller abstract classes which do one thing each (e.g. TextReadable, TextWriteable, TextSeekable)?

@o11c
Copy link
Contributor

o11c commented Oct 20, 2015

Currently, the python2 stubs for cStringIO are forced to lie because read/write methods are not split into separate base classes. Doubtless there are many other cases, both in python2 and python3.

Suggest retitling to IO[AnyStr].

@srittau srittau added topic: feature Discussions about new features for Python's type annotations resolution: out of scope The idea or discussion was out of scope for this repository and removed enhancement labels Nov 4, 2021
@srittau
Copy link
Collaborator

srittau commented Nov 4, 2021

I believe this issue is obsolete by now, due to protocols and the suggestion to use protocols for I/O argument types.

@srittau srittau closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution: out of scope The idea or discussion was out of scope for this repository topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

4 participants