Python 3: Sphinx doesn't show type hints correctly - python-sphinx

I am generating a HTML documentation of a Python 3 module automatically from its reStructuredText docstrings of its functions by using Sphinx (make HTML). The generated HTML documentation looks fine so far, but the parameter types of the function signatures, which are given in the source code as PEP484 type hints aren't shown correctly.
E.g. this is some example output from the Sphinx-generated HTML doc of one of my functions:
static parse_from_file(filename: str) → list
Parses stuff from a text file.
Parameters: filename – the filepath of a textfile to be parsed
Returns: list of parsed elements
This is what I would expect it to look like:
static parse_from_file(filename)
Parses stuff from a text file.
Parameters: filename (str) – the filepath of a textfile to be parsed
Returns: list of parsed elements
Return type: list
This is how the Python code actually looks like:
def parse_from_file(filename: str) -> list:
"""Parses stuff from a text file.
:param filename: the filepath of a textfile to be parsed
:returns: list of parsed elements
"""
return []
How can I make Sphinx show the Python 3 type hints correctly?

I tackled it on my own by using the sphinx-autodoc-typehints extension.

There is autodoc_typehints variable. Since version 3.0 you can set it to 'description' which shows typehints as content of function or method and removes them from signature.
So just add this line into your conf.py:
autodoc_typehints = "description"

Related

How i can Insert contents of text file with heredoc and variable with ruby

There was a problem when im creating the CLI. I'm want to give the user the opportunity to insert their data into a text file, for this I created a file and added a heredoc to it
I'm trying to get data from a text document that has a heredoc inside of it with a function that is supposed to interpolate
When I try to display the result of the file, I get the entire contents of the file, including the heredoc
an example will be below
I tried to solve my problem through File class
variable_name = File::open("path_directory/file_with_heredoc.txt", "r+")::read
Next, I decided to give the value of the variable to the terminal via
exec("echo #{variable_name}")
The terminal displays
file = <<-EOM
single text with def result: #{upcase_def("Hello")}
EOM
Tried to give through struct, but result is unchanged
exec("echo #{variable_name.strip}")
What do I need to do to get only data, no HEREDOC syntax?
I want to get this result
"single text with def result: HELLO"
I think this is what you are trying to do but I recommend you to first do some research why 'eval() is evil'. If the file is a user (or hacker) input you definitely want some sanitization there or a completely different approach.
def upcase_def(str)
str.upcase
end
data = File.read('file_with_heredoc.txt')
eval(data)
# => " single text with def result: HELLO\n"

Replacing variable value in ruby while setting the value using "set" command

I have a .properties files as below:
user:abcd
pwd:xyz
system:test
Next, I have a ruby script with Watir for browser automation. In this script, I have statements like
browser.text_field(:id => 'identifierId').set "#{user}:variable to be replaced by its value from .properties file".
Similarly, other values need to be replaced for "pwd" and "system".
I tried the solution per below posts:
Replace properties in one file from those in another in Ruby
However, "set" command is setting whatever has been paased as arguments to it instead of replacing the variable with its value.
Please help.
You have to read the information out of the file.
Most Watir users leverage yaml files for this.
config/properties.yml:
user: abcd
pwd: xyz
system: test
Then read the yaml file & parse your data:
properties = YAML.safe_load(IO.read('config/properties.yml'))
text_field = browser.text_field(id: 'identifierId')
text_field.set properties['user']
Alternately you can take a look at Cheezy's Fig Newton gem, which is designed to work with his Page Object gem

Replace properties in one file from those in another in Ruby

I want to replace properties in one file from those in another. (I am new to ruby, and read about Ruby and YAML. I have a Java background)
Eg.
File 1
server_ip_address=$[ip]
value_threshold=$[threshold]
system_name=$[sys_name]
File 2
ip=192.168.1.1
threshold=10
sys_name=foo
The ruby script should replace the $ values by their real values (I do not know if $[] is the format used in ruby. Also do Files 1 and 2 have to be YAML files, or erb files?) and produce File 1 as :
server_ip_address=192.168.1.1
value_threshold=10
system_name=foo
I searched the web for this, but could not express it in the right keywords to find a solution/pointer to a solution/reference material on google. How can this be done by a ruby script?
Thanks
If you can switch the formats, this should be as easy as:
require 'yaml'
variables = YAML.load(File.open('file2.yaml'))
template = File.read('file1.conf')
puts template.gsub(/\$\[(\w+)\]/) { variables[$1] }
Your template can stay as-is, but the substitution file would look like:
ip: 192.168.1.1
threshold: 10
sys_name: foo
This makes it easy to read in using the YAML library.

Ruby/Slim: parse Markdown from a YAML file

Been struggling for a while with some YAML parsing inside a Slim template.
my YAML file contain
shortdesc: >
markdown:
if you want to up the feelgood factor Cuban style, then this Monday night at The Buffalo Bar is for you...
But when I output the shortdesc node in my template it's displayed as a string and not interpreted. ("markdown: if you....")
Is there a way to parse the YAML output string to interpret the markdown code?
If I try
p
markdown:
= shortdesc
the template doesn't understand the call to the variable containing the YAML node.
Is that even possible?
It depends on the Markdown Library that you are using.
In BlueCloth, it would be something like this:
= BlueCloth.new(shortdesc).to_html
Yes it's possible. Just need to use interpolation:
p
markdown:
#{shortdesc}

Ruby Premature EOF?

I'm trying to write one file into another one in Ruby, but the output seems to stop prematurely.
Input file - large CSS file with base64 embedded fonts
Output file - basic html file.
#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
while !ext_css_file.eof()
out_file.puts(ext_css_file.read())
end
...
#write some HTML after the CSS (works)
The resulting file is basically a valid HTML file, with a truncated CSS (in the middle of an embedded font)
When doing a puts on the result of read(), I get the same result: The CSS file is read only up to this last string: "RMSHhoPCAGt/mELDBESFBQSggGfAgESKCUAAAAAAAwAlgABAAAAAAABAAUADAABAAAAAAAC"
It is difficult to provide a detailed solution without more insight into what the CSS file actually contains. Based on your code above, I would try something like this instead:
#write some HTML before the CSS (works)
...
#write the external CSS (doesn't work, output finished prematurely)
out_file.puts(ext_css_file.read())
...
#write some HTML after the CSS (works)
I don't think you need the .eof check because the read method reads and returns the entire file contents, or an empty string or nil if at the end of file. See here: http://apidock.com/ruby/IO/read
I would tend to read and write the same type of data. For instance if I were writing data into the new file using puts, I would read data using readlines. If I were writing binary data using write, I would read the data using read. I would be consistent with either strings or bytes and not mix the two.
Try something like this...
File.open('writable_file_path', 'w') do |f|
# f.puts "some html"
f.puts IO.readlines('css_file_path')
# f.puts "some more html"
end

Resources