Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions/checkout-3
Browse files Browse the repository at this point in the history
  • Loading branch information
devPalacio committed Jul 20, 2023
2 parents 064ed6a + af56116 commit d23fd4f
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# https://github.com/actions/python-versions/blob/main/versions-manifest.json
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-latest, ubuntu-20.04]
python-version: [3.6, 3.7, 3.8, pypy-3.7]
steps:
- uses: actions/checkout@v3
Expand All @@ -21,7 +21,7 @@ jobs:
- name: Install Requirements
run: |
python -m pip install --upgrade pip
pip install flake8 pylint pytest
pip install flake8==5.0.4 pylint pytest
pip install -r requirements.txt
pip install -r test/requirements.txt
python setup.py install
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ command line::

$ stone -h

Stone requires Python 3.

Alternative
-----------

Expand Down
1 change: 0 additions & 1 deletion mypy-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ EXCLUDE='(^example/|^ez_setup\.py$|^setup\.py$)'
INCLUDE=$(git ls-files "$@" | grep '\.py$' | grep -Ev "$EXCLUDE" | tr '\n' '\0' | xargs -0 | cat)
MYPY_CMD=mypy
$MYPY_CMD $INCLUDE
$MYPY_CMD --py2 $INCLUDE
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[mypy]
# follow_imports = False
ignore_missing_imports = True
show_none_errors = True
strict_optional = True
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
install_reqs += f.read().splitlines()

setup_requires = [
# Pin pytest-runner to 5.2.0, since 5.3.0 uses `find_namespaces` directive, not supported in
# Python 2.7
'pytest-runner == 5.2.0',
'pytest-runner == 5.3.2',
]

# WARNING: This imposes limitations on test/requirements.txt such that the
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/js_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression
from stone.ir import ApiNamespace

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

from stone.backend import CodeBackend
from stone.backends.js_helpers import (
Expand Down
9 changes: 1 addition & 8 deletions stone/backends/js_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any

import argparse

_cmdline_parser = argparse.ArgumentParser(prog='js-types-backend')
_cmdline_parser.add_argument(
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/obj_c_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

_cmdline_parser = argparse.ArgumentParser(
prog='objc-client-backend',
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/obj_c_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

_cmdline_parser = argparse.ArgumentParser(prog='obj-c-types-backend')
_cmdline_parser.add_argument(
Expand Down
9 changes: 1 addition & 8 deletions stone/backends/python_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any

import argparse

# This will be at the top of the generated file.
base = """\
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/swift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse


_cmdline_parser = argparse.ArgumentParser(
Expand Down
9 changes: 1 addition & 8 deletions stone/backends/swift_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any

import argparse

_cmdline_parser = argparse.ArgumentParser(prog='swift-types-backend')
_cmdline_parser.add_argument(
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/tsd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

from stone.backend import CodeBackend
from stone.backends.tsd_helpers import (
Expand Down
8 changes: 1 addition & 7 deletions stone/backends/tsd_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

from stone.ir import ApiNamespace
from stone.ir import (
Expand Down
32 changes: 20 additions & 12 deletions stone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import codecs
import imp # pylint: disable=deprecated-module
import importlib
import io
import json
import logging
Expand All @@ -26,13 +26,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any
import argparse

# These backends come by default
_builtin_backends = (
Expand Down Expand Up @@ -143,10 +137,8 @@
help='If set, backends will not see any routes for the specified namespaces.',
)


def main():
"""The entry point for the program."""

if '--' in sys.argv:
cli_args = sys.argv[1:sys.argv.index('--')]
backend_args = sys.argv[sys.argv.index('--') + 1:]
Expand Down Expand Up @@ -175,7 +167,8 @@ def main():
# The module should should contain an api variable that references a
# :class:`stone.api.Api` object.
try:
api = imp.load_source('api', args.api[0]).api # pylint: disable=redefined-outer-name
api_module = _load_module('api', args.api[0])
api = api_module.api # pylint: disable=redefined-outer-name
except ImportError as e:
print('error: Could not import API description due to:',
e, file=sys.stderr)
Expand Down Expand Up @@ -348,7 +341,7 @@ def main():
if new_python_path not in sys.path:
sys.path.append(new_python_path)
try:
backend_module = imp.load_source('user_backend', args.backend)
backend_module = _load_module('user_backend', args.backend)
except Exception:
print("error: Importing backend '%s' module raised an exception:" %
args.backend, file=sys.stderr)
Expand All @@ -374,6 +367,21 @@ def main():
# easier to do debugging.
return api

def _load_module(name, path):
file_name = os.path.basename(path)
module_name = file_name.replace('.', '_')

if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
module_specs = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(module_specs)
module_specs.loader.exec_module(module)
else:
loader = importlib.machinery.SourceFileLoader(module_name, path)
module = loader.load_module() # pylint: disable=deprecated-method,no-value-for-parameter

sys.modules[name] = module

return module

if __name__ == '__main__':
# Assign api variable for easy debugging from a Python console
Expand Down
19 changes: 9 additions & 10 deletions stone/frontend/ir_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
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 # type: ignore

_MYPY = False
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
re = importlib.import_module(str('re')) # type: typing.Any
import re

from ..ir import (
Alias,
Expand Down Expand Up @@ -1074,7 +1073,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 +1153,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
2 changes: 1 addition & 1 deletion stone/ir/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
# See <https://github.com/PyCQA/pylint/issues/73>
from distutils.version import StrictVersion
from distutils.version import StrictVersion # pylint: disable=deprecated-module
import six

from .data_types import (
Expand Down
6 changes: 2 additions & 4 deletions stone/ir/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def set_default(self, default):
@property
def default(self):
if not self.has_default:
raise Exception('Type has no default')
raise ValueError('Type has no default')
else:
return self._default

Expand Down Expand Up @@ -1382,7 +1382,7 @@ def __init__(self, name, namespace, ast_node, closed):

# TODO: Why is this a different signature than the parent? Is this
# intentional?
def set_attributes(self, doc, fields, # pylint: disable=arguments-differ
def set_attributes(self, doc, fields,
parent_type=None, catch_all_field=None):
"""
:param UnionField catch_all_field: The field designated as the
Expand Down Expand Up @@ -1475,7 +1475,6 @@ def _add_example(self, example):
)

# TODO: are we always guaranteed at least one field?
# pylint: disable=undefined-loop-variable
try:
field.data_type.check_example(example_field)
except InvalidSpec as e:
Expand Down Expand Up @@ -1581,7 +1580,6 @@ def get_json_val(dt, val):
raise AssertionError('No example for label %r' % label)

# TODO: are we always guaranteed at least one field?
# pylint: disable=undefined-loop-variable
assert is_void_type(field.data_type)
return Example(
field.name, field.doc, OrderedDict([('.tag', field.name)]))
Expand Down
9 changes: 1 addition & 8 deletions test/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression

# Hack to get around some of Python 2's standard library modules that
# accept ascii-encodable unicode literals in lieu of strs, but where
# actually passing such literals results in errors with mypy --py2. See
# <https://github.com/python/typeshed/issues/756> and
# <https://github.com/python/mypy/issues/2536>.
import importlib
argparse = importlib.import_module(str('argparse')) # type: typing.Any

import argparse
class _Tester(CodeBackend):
"""A no-op backend used to test helper methods."""
def generate(self, api):
Expand Down
4 changes: 2 additions & 2 deletions test/test_python_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,8 +1245,8 @@ def test_struct_equality_with_value(self):
self.assertNotEqual(s, s_unequal)

def test_struct_equality_with_different_types(self):
"""Structs of different types that do not have an inheritance relationship are not considered
equal to each other."""
"""Structs of different types that do not have an inheritance relationship are not
considered equal to each other."""
s = self.decode(bv.Struct(self.ns.S), json.dumps({'f': 'F'}))
t = self.decode(bv.Struct(self.ns.T), json.dumps({'f': 'F'}))
self.assertNotEqual(s, t)
Expand Down
2 changes: 1 addition & 1 deletion test/test_tsd_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from test.backend_test_util import _mock_output


def _make_backend(target_folder_path, template_path, custom_args=None):
def _make_backend(target_folder_path, template_path, custom_args=None): # type: ignore
# type: (typing.Text, typing.Text, typing.List) -> TSDTypesBackend

args = Mock()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ commands =
pylint --rcfile=.pylintrc setup.py example stone test

deps =
flake8
flake8<6
pylint
# This probably breaks on Windows. See
# <https://github.com/tox-dev/tox/issues/384>.
Expand Down

0 comments on commit d23fd4f

Please sign in to comment.