Skip to content

Commit

Permalink
Version compatible inspect handling for class parameters (#299)
Browse files Browse the repository at this point in the history
* Version compatible inspect handling for class parameters

`getargspec` was deprecated in Python 3.0, but finally removed in Python
3.11. For reference: Pylons/pyramid#3688

That seems like a valid amount of time.

We can do our best effort and if the user is running a Python version
that still has `getargspec` use that, but if they have `getfullargspec`
which is available in Python 3.11, we can use that.

* Update ir_generator.py

include `isclass`
  • Loading branch information
johnlarkin1 committed May 18, 2023
1 parent bdd49bc commit 199029a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions stone/frontend/ir_generator.py
Expand Up @@ -3,9 +3,14 @@
from collections import defaultdict

import copy
import inspect
import logging

from inspect import isclass
try:
from inspect import getfullargspec as get_args
except ImportError:
from inspect import getargspec as get_args

_MYPY = False
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression
Expand Down Expand Up @@ -1074,7 +1079,7 @@ def _instantiate_data_type(self, data_type_class, data_type_args, loc):
assert issubclass(data_type_class, DataType), \
'Expected stone.data_type.DataType, got %r' % data_type_class

argspec = inspect.getargspec(data_type_class.__init__) # noqa: E501 # pylint: disable=deprecated-method,useless-suppression
argspec = get_args(data_type_class.__init__) # noqa: E501 # pylint: disable=deprecated-method,useless-suppression
argspec.args.remove('self')
num_args = len(argspec.args)
# Unfortunately, argspec.defaults is None if there are no defaults
Expand Down Expand Up @@ -1154,7 +1159,7 @@ def _resolve_type(self, env, type_ref, enforce_fully_defined=False):
if obj is Void and type_ref.nullable:
raise InvalidSpec('Void cannot be marked nullable.',
*loc)
elif inspect.isclass(obj):
elif isclass(obj):
resolved_data_type_args = self._resolve_args(env, type_ref.args)
data_type = self._instantiate_data_type(
obj, resolved_data_type_args, (type_ref.lineno, type_ref.path))
Expand Down

0 comments on commit 199029a

Please sign in to comment.