In python type-hinting, how can I make an argument accept any subclass of a base class?

Salim Fadhley

I have a function that looks a bit like this. I want the function to accept any subclass of io.IOBase - in other words, any file-like object.

def import_csv_file(f:io.IOBase)->pandas.DataFrame:
    return pandas.read_csv(f)

When I view the object in IntelliJ, the JetBrains implementation of type-hinting rejects any input unless I provide exactly an instance of io.IOBase - but what if I want to pass in an instance of a sub-class of io.IOBase? Is there a way to change the type-hint to say that this is allowed?

Eugene Yarmash

If you annotate a function argument with the base class (io.IOBase in your case) then you can also pass instances of any subtype of the base class – inheritance applies to annotation types as well.

That said, you could use typing.IO as a generic type representing any I/O stream (and typing.TextIO and typing.BinaryIO for binary and text I/O streams respectively).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make a Rust function accept any floating type as an argument

In PHPStorm, how can I make type hinting work when I have a superclass method that returns a different type from each subclass

When using the 'Class' datatype, how can I specify the type so I only accept subclass of a specific class?

How can I make a method accept a class type variable in Java?

How to add type hinting to "class" type in python

How can I properly type a subclass of class which sub-types a Generic base class?

Python type hinting: how to tell X is a subclass for Foo?

How can I pass subclass to proc expecting argument of parent type?

How can I pass a subclass as an argument for a method that wants the super class?

How do I make a function accept any type (not necessarily an object)?

when I make custom exception class, How can I invokes the base constructor with the provided argument?

Python Type Hinting: Subclass not compatible with generic

How can I make python's argparse accept any number of [-R a b]s, and aggregate them into a list

How can I make the input accept any form of the words in the brackets?

How to specify several string options for an argument in Python Type Hinting

In TypeScript, how can I return the subclass from the base class?

How can I extend class parameters with a method while preserving type hinting in typescript?

Python class enforce type hinting

Subclass in type hinting

Python type hinting for async function as function argument

How can I get format to not cause a type-hinting error?

How do I get `mypy` to recognize that an argument of a function needs to be a subclass of a particular base class?

How do I get mypy to recognize that an argument of a function needs to be a subclass of a particular base class?

Accept Any type of argument in cpp

why C++ template type matching does not match to base class refrence, how can I make it match to base class refrence?

How can I make the new python accept print without parentheses?

How to make a Java class that can take parameters of any type

Subclass not valid as input to list of Superclass / type hinting in python

How can I learn actual type argument of an generic class?