Skip to content

Commit

Permalink
Fix STRING_VEC default arguments presentation in docs (NVIDIA#3470)
Browse files Browse the repository at this point in the history
Adjust python_repr to handle vectors of elements,
as it was currently dead code. We need to call repr
on all strings as we do for single string argument.

Add backticks around the default value in the generated
docstring - it prevents string from interpreting
glob expressions as markdown.

Signed-off-by: Krzysztof Lecki <[email protected]>
  • Loading branch information
klecki authored Nov 4, 2021
1 parent 2813d67 commit cd932e7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
11 changes: 7 additions & 4 deletions dali/pipeline/operator/op_schema.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2017-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -189,11 +189,14 @@ std::string OpSchema::GetArgumentDefaultValueString(const std::string &name) con

auto &val = *value_ptr;
auto str = val.ToString();
if (val.GetTypeId() == DALI_STRING ||
val.GetTypeId() == DALI_TENSOR_LAYOUT)
if (val.GetTypeId() == DALI_STRING || val.GetTypeId() == DALI_TENSOR_LAYOUT) {
return python_repr(str);
else
} else if (val.GetTypeId() == DALI_STRING_VEC) {
auto str_vec = dynamic_cast<ValueInst<std::vector<std::string>> &>(val).Get();
return python_repr(str_vec);
} else {
return str;
}
}

std::vector<std::string> OpSchema::GetArgumentNames() const {
Expand Down
2 changes: 1 addition & 1 deletion dali/python/nvidia/dali/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _get_kwargs(schema):
if schema.HasArgumentDefaultValue(arg):
default_value_string = schema.GetArgumentDefaultValueString(arg)
default_value = ast.literal_eval(default_value_string)
type_name += ", default = {}".format(_default_converter(dtype, default_value))
type_name += ", default = `{}`".format(_default_converter(dtype, default_value))
doc += schema.GetArgumentDox(arg)
if deprecation_warning:
doc += "\n\n" + deprecation_warning
Expand Down
9 changes: 5 additions & 4 deletions include/dali/core/python_util.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2019-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,10 @@
#ifndef DALI_CORE_PYTHON_UTIL_H_
#define DALI_CORE_PYTHON_UTIL_H_

#include <string>
#include <sstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "dali/core/util.h"

namespace dali {
Expand Down Expand Up @@ -74,7 +75,7 @@ inline void python_repr(std::ostream &os, const std::string &s) {
}

template <typename T>
if_iterable<T> python_repr(std::ostream &os, const T &collection) {
void python_repr(std::ostream &os, const std::vector<T> &collection) {
os << "[";
bool first = true;
for (const auto &v : collection) {
Expand Down

0 comments on commit cd932e7

Please sign in to comment.