Python isinstance: check a type
isinstance(x, T) is true when x is an instance of T or of a subclass of T. T can be a tuple of types. Other class checks were not run.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-96-A: isinstance with a type, with a tuple of types, and with bool as a subclass of int
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
print(isinstance(3, int))
print(isinstance('3', (int, float)))
print(isinstance(3, (int, float)))
print(isinstance(True, int))
EOSCommand:
python3 s.pyRecorded output:
True
False
True
TrueExit status: 0. The output matches the expected text written before the run.
TE-96-B: isinstance is true for an instance of a user-defined subclass, and issubclass compares the classes
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
class A:
pass
class B(A):
pass
print(isinstance(B(), A))
print(isinstance(A(), B))
print(issubclass(B, A))
EOSCommand:
python3 s.pyRecorded output:
True
False
TrueExit status: 0. This status and the output match what was written before the run.
TE-96-C: expected non-zero exit: isinstance with a string instead of a type raises TypeError
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
isinstance(3, 'int')
EOSCommand:
python3 s.pyRecorded output: none, standard output was empty.
Standard error from the same run:
Traceback (most recent call last):
File "/tmp/b24-cp3mtrvp/work/s.py", line 1, in <module>
isinstance(3, 'int')
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a unionExit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
TE-96-B: an instance of B counted as an instance of A, an instance of A did not count as an instance of B, and issubclass(B, A) printed True. TE-96-C: isinstance(3, 'int') stopped with exit status 1. The last line of standard error is TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union.
Limits
bool is a subclass of int, so isinstance(True, int) is true, which the run shows. TE-96-B runs one user-defined subclass. Other class checks were not run. Not tested on this page: calling isinstance with its parameters passed as keywords. The help text of Python 3.10.12 writes the signature as isinstance(obj, class_or_tuple, /) and says a tuple target is equivalent to joining one isinstance check per class with or. No run here used keyword arguments. The help text names a tuple as the target; an empty tuple and a tuple nested inside a tuple were not run.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-96-A | isinstance with a type, with a tuple of types, and with bool as a subclass of int | 0 | PASS | 56dcc1ebff3f | | TE-96-B | isinstance is true for an instance of a user-defined subclass, and issubclass compares the classes | 0 | PASS | b9b65d4869d6 | | TE-96-C | expected non-zero exit: isinstance with a string instead of a type raises TypeError | 1 | PASS | 5346ee262616 |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides