Skip to content

Commit

Permalink
Merge pull request #1563 from rstudio/black-py-files
Browse files Browse the repository at this point in the history
Format Python files with Black
  • Loading branch information
t-kalinowski authored Mar 28, 2024
2 parents 8a25773 + 8e1e0b7 commit bf21d1d
Show file tree
Hide file tree
Showing 10 changed files with 533 additions and 449 deletions.
16 changes: 16 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Format Python files with Black
fc430b4dbf1854b68de44d927add8a41f64beecc

# whitespace only commits
e1df4e54c0a4d15cbb01a3af7e8466d29ed64305
02a0d984171dcacd322e7c071edf3d6b1cefb7b1
95573e7f3e0a11e4d703c4dbc6c29a409ee7b242
64acdfa98c29c70917ca7f60f05ad4192272e244
da67e9dff1be9b5c344aa3431eae520b57142bde
1a62650d952460ce047e06354b84d1a3b3e683de


## Snippet to inspect/filter commits quickly:
# library(tidyverse)
# df <- as_tibble(git2r::repository("."))
# df |> filter(grepl("white[- ]?space", summary))
62 changes: 34 additions & 28 deletions inst/config/config.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@

import platform
import sys
import os

# The 'sysconfig' module is only available with Python 2.7 and newer, but
# an equivalent module in 'distutils' is available for Python 2.6.
if sys.version_info < (2, 7):
from distutils import sysconfig
from distutils import sysconfig
else:
import sysconfig
import sysconfig

# The 'imp' module is deprecated since Python 3.4, and the use of
# 'importlib' is recommended instead.
if sys.version_info < (3, 4):
import imp
def module_path(name):
if name in sys.builtin_module_names:
return "[builtin module]"
spec = imp.find_module(name)
return spec[1]
import imp

def module_path(name):
if name in sys.builtin_module_names:
return "[builtin module]"
spec = imp.find_module(name)
return spec[1]

else:
from importlib import util
def module_path(name):
if name in sys.builtin_module_names:
return "[builtin module]"
spec = util.find_spec(name)
origin = spec.origin
return origin[:origin.rfind('/')]
from importlib import util

def module_path(name):
if name in sys.builtin_module_names:
return "[builtin module]"
spec = util.find_spec(name)
origin = spec.origin
return origin[: origin.rfind("/")]


# Get appropriate path-entry separator for platform
pathsep = ";" if os.name == "nt" else ":"

# Read default configuration values
# fmt: off
config = {
"Architecture" : platform.architecture()[0],
"Version" : str(sys.version).replace("\n", " "),
Expand All @@ -46,31 +50,33 @@ def module_path(name):
"Executable" : getattr(sys, "executable", ""),
"BaseExecutable" : getattr(sys, "_base_executable", ""),
}
# fmt: on

# detect if this is a conda managed python
# https://stackoverflow.com/a/21282816/5128728
if sys.version_info >= (3, 7):
is_conda = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
is_conda = os.path.exists(os.path.join(sys.prefix, "conda-meta"))
else:
is_conda = 'conda' in sys.version
config['IsConda'] = is_conda
is_conda = "conda" in sys.version
config["IsConda"] = is_conda

# Read numpy configuration (if available)
try:
import numpy
config["NumpyPath"] = str(numpy.__path__[0])
config["NumpyVersion"] = str(numpy.__version__)
import numpy

config["NumpyPath"] = str(numpy.__path__[0])
config["NumpyVersion"] = str(numpy.__version__)
except:
pass
pass

# Read required module information (if requested)
try:
required_module = os.environ["RETICULATE_REQUIRED_MODULE"]
if required_module is not None and len(required_module) > 0:
config["RequiredModule"] = required_module
config["RequiredModulePath"] = module_path(required_module)
required_module = os.environ["RETICULATE_REQUIRED_MODULE"]
if required_module is not None and len(required_module) > 0:
config["RequiredModule"] = required_module
config["RequiredModulePath"] = module_path(required_module)
except:
pass
pass

# Write configuration to stdout
lines = [str(key) + ": " + str(val) for (key, val) in config.items()]
Expand Down
4 changes: 3 additions & 1 deletion inst/python/rpytools/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def _tend_queue(self, min_fetch=0):
if threading.current_thread() is not threading.main_thread():
if not self._pending_tend_queue:
self._pending_tend_queue = True
rpycall.call_python_function_on_main_thread(self._tend_queue, min_fetch)
rpycall.call_python_function_on_main_thread(
self._tend_queue, min_fetch
)
return

# We're on the main thread, call the generator and put values on the queue.
Expand Down
196 changes: 104 additions & 92 deletions inst/python/rpytools/help.py
Original file line number Diff line number Diff line change
@@ -1,140 +1,152 @@

import sys
import types
import inspect


def isstring(s):
# if we use Python 3
if (sys.version_info[0] >= 3):
if sys.version_info[0] >= 3:
return isinstance(s, str)
# we use Python 2
return isinstance(s, basestring)


def normalize_func(func):
# return None for builtins
if (inspect.isbuiltin(func)):
if inspect.isbuiltin(func):
return None
return func


def get_doc(func):
doc = inspect.getdoc(func)
if doc is None:
func = normalize_func(func)
if func is None:
return None
else:
doc = inspect.getdoc(func)
return doc
doc = inspect.getdoc(func)
if doc is None:
func = normalize_func(func)
if func is None:
return None
else:
doc = inspect.getdoc(func)
return doc


def get_property_doc(target, prop):
for name, obj in inspect.getmembers(type(target), inspect.isdatadescriptor):
if (isinstance(obj, property) and name == prop):
return inspect.getdoc(obj.fget)
return None
for name, obj in inspect.getmembers(type(target), inspect.isdatadescriptor):
if isinstance(obj, property) and name == prop:
return inspect.getdoc(obj.fget)
return None


def get_argspec(func):
try:
if sys.version_info[0] >= 3:
return inspect.getfullargspec(func)
else:
return inspect.getargspec(func)
except TypeError:
return None
try:
if sys.version_info[0] >= 3:
return inspect.getfullargspec(func)
else:
return inspect.getargspec(func)
except TypeError:
return None


def get_arguments(func):
func = normalize_func(func)
if func is None:
return None
return None
argspec = get_argspec(func)
if argspec is None:
return None
return None
args = argspec.args
if 'self' in args:
args.remove('self')
if "self" in args:
args.remove("self")
return args


def get_r_representation(default):
if callable(default) and hasattr(default, '__name__'):
arg_value = default.__name__
else:
if default is None:
arg_value = "NULL"
elif type(default) == type(True):
if default == True:
arg_value = "TRUE"
else:
arg_value = "FALSE"
elif isstring(default):
arg_value = "\"%s\"" % default
elif isinstance(default, int):
arg_value = "%rL" % default
elif isinstance(default, float):
arg_value = "%r" % default
elif isinstance(default, list):
arg_value = "c("
for i, item in enumerate(default):
if i is (len(default) - 1):
arg_value += "%s)" % get_r_representation(item)
else:
arg_value += "%s, " % get_r_representation(item)
elif isinstance(default, (tuple, set)):
arg_value = "list("
for i, item in enumerate(default):
if i is (len(default) - 1):
arg_value += "%s)" % get_r_representation(item)
else:
arg_value += "%s, " % get_r_representation(item)
elif isinstance(default, dict):
dict_entries = ", ".join([
("%s = %s" % (k, get_r_representation(v)))
for k, v in default.items()
])
arg_value = "list(" + dict_entries + ")"
if callable(default) and hasattr(default, "__name__"):
arg_value = default.__name__
else:
arg_value = "%r" % default

# if the value starts with "tf." then convert to $ usage
if (arg_value.startswith("tf.")):
arg_value = arg_value.replace(".", "$")

return(arg_value)
if default is None:
arg_value = "NULL"
elif type(default) == type(True):
if default == True:
arg_value = "TRUE"
else:
arg_value = "FALSE"
elif isstring(default):
arg_value = '"%s"' % default
elif isinstance(default, int):
arg_value = "%rL" % default
elif isinstance(default, float):
arg_value = "%r" % default
elif isinstance(default, list):
arg_value = "c("
for i, item in enumerate(default):
if i is (len(default) - 1):
arg_value += "%s)" % get_r_representation(item)
else:
arg_value += "%s, " % get_r_representation(item)
elif isinstance(default, (tuple, set)):
arg_value = "list("
for i, item in enumerate(default):
if i is (len(default) - 1):
arg_value += "%s)" % get_r_representation(item)
else:
arg_value += "%s, " % get_r_representation(item)
elif isinstance(default, dict):
dict_entries = ", ".join(
[
("%s = %s" % (k, get_r_representation(v)))
for k, v in default.items()
]
)
arg_value = "list(" + dict_entries + ")"
else:
arg_value = "%r" % default

# if the value starts with "tf." then convert to $ usage
if arg_value.startswith("tf."):
arg_value = arg_value.replace(".", "$")

return arg_value


def generate_signature_for_function(func):
"""Given a function, returns a string representing its args."""

func = normalize_func(func)
if func is None:
return None
return None

args_list = []

argspec = get_argspec(func)
if argspec is None:
return None

first_arg_with_default = (
len(argspec.args or []) - len(argspec.defaults or []))
return None

first_arg_with_default = len(argspec.args or []) - len(
argspec.defaults or []
)
for arg in argspec.args[:first_arg_with_default]:
if arg == "self":
# Python documentation typically skips `self` when printing method
# signatures.
continue
args_list.append(arg)
if arg == "self":
# Python documentation typically skips `self` when printing method
# signatures.
continue
args_list.append(arg)

if argspec.varargs == "args" and hasattr(argspec, 'keywords') and argspec.keywords == "kwds":
original_func = func.__closure__[0].cell_contents
return generate_signature_for_function(original_func)
if (
argspec.varargs == "args"
and hasattr(argspec, "keywords")
and argspec.keywords == "kwds"
):
original_func = func.__closure__[0].cell_contents
return generate_signature_for_function(original_func)

if argspec.defaults:
for arg, default in zip(
argspec.args[first_arg_with_default:], argspec.defaults):
arg_value = get_r_representation(default)
args_list.append("%s = %s" % (arg, arg_value))
for arg, default in zip(
argspec.args[first_arg_with_default:], argspec.defaults
):
arg_value = get_r_representation(default)
args_list.append("%s = %s" % (arg, arg_value))
if argspec.varargs:
args_list.append("...")
if hasattr(argspec, 'keywords') and argspec.keywords:
args_list.append("...")
args_list.append("...")
if hasattr(argspec, "keywords") and argspec.keywords:
args_list.append("...")
return "(" + ", ".join(args_list) + ")"

Loading

0 comments on commit bf21d1d

Please sign in to comment.