Clean up, remove compat with py < 3.6
[pyi2ncommon] / src / type_helpers.py
CommitLineData
1ee3821a
CH
1# The software in this package is distributed under the GNU General
2# Public License version 2 (with a special exception described below).
3#
4# A copy of GNU General Public License (GPL) is included in this distribution,
5# in the file COPYING.GPL.
6#
7# As a special exception, if other files instantiate templates or use macros
8# or inline functions from this file, or you compile this file and link it
9# with other works to produce a work based on this file, this file
10# does not by itself cause the resulting work to be covered
11# by the GNU General Public License.
12#
13# However the source code for this file must still be made available
14# in accordance with section (3) of the GNU General Public License.
15#
16# This exception does not invalidate any other reasons why a work based
17# on this file might be covered by the GNU General Public License.
f365f614
CH
18#
19# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
1ee3821a
CH
20
21"""
7628bc48
CH
22Helpers for type checking and conversion, like `isstr(x)`, `is_file_obj(x)`
23
24Some of these things used to be more complicated when python2 was still
25around, so some functions are not that useful anymore.
1ee3821a
CH
26"""
27
00e0ebd2 28from io import IOBase
1ee3821a 29
1ee3821a
CH
30
31def isstr(var):
7628bc48 32 """Alias for `isinstance(var, str)`."""
2b141a0a 33 return isinstance(var, str)
00e0ebd2
CH
34
35
36def is_str_or_byte(var):
7628bc48 37 """Returns `True` for `str` and `bytes` objects."""
2b141a0a 38 return isinstance(var, (str, bytes))
00e0ebd2
CH
39
40
e8de64e2 41def is_unicode(var):
7628bc48 42 """Alias for `isinstance(var, str)`."""
2b141a0a 43 return isinstance(var, str)
e8de64e2
CH
44
45
00e0ebd2 46def is_file_obj(var):
7628bc48
CH
47 """
48 Determine whether given input is the result of 'open(file_name)'.
00e0ebd2 49
7628bc48
CH
50 Just checks whether given var is subclass of io.IOBase, which is also True
51 for 'file-like objects' like StringIO.
00e0ebd2 52 """
2b141a0a 53 return isinstance(var, IOBase)