feat(python): fixed bugs + added unittests (#189)

This commit is contained in:
Colin Kennedy
2024-08-02 07:21:10 -07:00
committed by GitHub
parent b2942f546c
commit 8614bcb6c6
4 changed files with 127 additions and 76 deletions

View File

@@ -256,6 +256,13 @@ Tip: Take a look at this beatiful diagram, showing a representation of the codeb
![Visualization of this repo](./diagram.svg) ![Visualization of this repo](./diagram.svg)
## Tests
Run tests using this command
```sh
make test
```
## GIFS ## GIFS
![screen1](https://user-images.githubusercontent.com/5306901/135055052-6ee6a5e8-3f30-4c41-872e-e624e21a1e98.gif) ![screen1](https://user-images.githubusercontent.com/5306901/135055052-6ee6a5e8-3f30-4c41-872e-e624e21a1e98.gif)

View File

@@ -38,6 +38,30 @@ local get_nearest_parent = function(node, type_name)
end end
--- Remove raise (aka throw) nodes so there is only one node per-type.
---
---@param nodes table<string, TSNode[]> The nodes to simplify / reduce.
local deduplicate_throw_nodes = function(nodes)
if not nodes[i.Throw] then
return
end
local output = {}
local seen = {}
for _, node in ipairs(nodes[i.Throw]) do
local text = helpers.get_node_text(node)[1]
if not vim.tbl_contains(seen, text) then
table.insert(seen, text)
table.insert(output, node)
end
end
nodes[i.Throw] = output
end
--- Modify `nodes` if the found return(s) are **all** bare-returns. --- Modify `nodes` if the found return(s) are **all** bare-returns.
--- ---
--- A bare-return is used to return early from a function and aren't meant to be --- A bare-return is used to return early from a function and aren't meant to be
@@ -253,6 +277,8 @@ return {
validate_yield_nodes(nodes) validate_yield_nodes(nodes)
deduplicate_throw_nodes(nodes)
local res = extractors:extract_from_matched(nodes) local res = extractors:extract_from_matched(nodes)
res[i.Tparam] = temp[i.Tparam] res[i.Tparam] = temp[i.Tparam]
@@ -292,7 +318,7 @@ return {
return (t[i.Parameter] or t[i.Tparam]) and { true } return (t[i.Parameter] or t[i.Tparam]) and { true }
end, end,
[i.HasReturn] = function(t) [i.HasReturn] = function(t)
return (t[i.ReturnTypeHint] or t[i.Return]) and { true } return (not t[i.Yield] and (t[i.ReturnTypeHint] or t[i.Return]) and { true })
end, end,
[i.HasThrow] = function(t) [i.HasThrow] = function(t)
return t[i.Throw] and { true } return t[i.Throw] and { true }

View File

@@ -309,7 +309,7 @@ end
--- with multiple annotation conventions. --- with multiple annotation conventions.
---@tag neogen-changelog ---@tag neogen-changelog
---@toc_entry Changes in neogen plugin ---@toc_entry Changes in neogen plugin
neogen.version = "2.19.0" neogen.version = "2.19.1"
--minidoc_afterlines_end --minidoc_afterlines_end
return neogen return neogen

View File

@@ -460,43 +460,42 @@ describe("python: google_docstrings", function()
assert.equal(expected, result) assert.equal(expected, result)
end) end)
-- TODO: This is broken. Fix it! it("lists only one entry per-raised type", function()
-- This used to work but broke later on, it seems - https://github.com/danymat/neogen/pull/142 local source = [[
-- it("lists only one entry per-raised type", function() def foo(bar):|cursor|
-- local source = [[ if bar:
-- def foo(bar):|cursor| raise TypeError("THING")
-- if bar:
-- raise TypeError("THING") if GLOBAL:
-- raise ValueError("asdffsd")
-- if GLOBAL:
-- raise ValueError("asdffsd") raise TypeError("BLAH")
-- ]]
-- raise TypeError("BLAH")
-- ]] local expected = [[
-- def foo(bar):
-- local expected = [[ """[TODO:description]
-- def foo(bar):
-- """[TODO:description] Args:
-- bar ([TODO:parameter]): [TODO:description]
-- Args:
-- bar ([TODO:parameter]): [TODO:description] Raises:
-- TypeError: [TODO:throw]
-- Raises: ValueError: [TODO:throw]
-- TypeError: [TODO:throw] """
-- ValueError: [TODO:throw] if bar:
-- """ raise TypeError("THING")
-- if bar:
-- raise TypeError("THING") if GLOBAL:
-- raise ValueError("asdffsd")
-- raise ValueError("asdffsd")
-- raise TypeError("BLAH")
-- raise TypeError("BLAH") ]]
-- ]]
-- local result = make_google_docstrings(source)
-- local result = _make_python_docstring(source)
-- assert.equal(expected, result)
-- assert.equal(expected, result) end)
-- end)
it("works with modules, even if they are nested", function() it("works with modules, even if they are nested", function()
local source = [[ local source = [[
@@ -540,43 +539,41 @@ describe("python: google_docstrings", function()
assert.equal(expected, result) assert.equal(expected, result)
end) end)
-- TODO: This is broken. Fix it! it("works with 2+ raises", function()
-- This used to work but broke later on, it seems - https://github.com/danymat/neogen/pull/142 local source = [[
-- it("works with 2+ raises", function() def foo(bar):|cursor|
-- local source = [[ if bar:
-- def foo(bar):|cursor| raise TypeError("THING")
-- if bar:
-- raise TypeError("THING") if GLOBAL:
-- raise TypeError("asdffsd")
-- if GLOBAL:
-- raise TypeError("asdffsd") raise TypeError("BLAH")
-- ]]
-- raise TypeError("BLAH")
-- ]] local expected = [[
-- def foo(bar):
-- local expected = [[ """[TODO:description]
-- def foo(bar):
-- """[TODO:description] Args:
-- bar ([TODO:parameter]): [TODO:description]
-- Args:
-- bar ([TODO:parameter]): [TODO:description] Raises:
-- TypeError: [TODO:throw]
-- Raises: """
-- TypeError: [TODO:throw] if bar:
-- """ raise TypeError("THING")
-- if bar:
-- raise TypeError("THING") if GLOBAL:
-- raise TypeError("asdffsd")
-- if GLOBAL:
-- raise TypeError("asdffsd") raise TypeError("BLAH")
-- ]]
-- raise TypeError("BLAH")
-- ]] local result = make_google_docstrings(source)
--
-- local result = _make_python_docstring(source) assert.equal(expected, result)
-- end)
-- assert.equal(expected, result)
-- end)
end) end)
describe("func - returns", function() describe("func - returns", function()
@@ -745,6 +742,27 @@ describe("python: google_docstrings", function()
assert.equal(expected, result) assert.equal(expected, result)
end) end)
it("works with type-hints as expected", function()
local source = [[
def foo() -> typing.Generator[str]:|cursor|
yield "asdfsfd"
]]
local expected = [[
def foo() -> typing.Generator[str]:
"""[TODO:description]
Yields:
[TODO:description]
"""
yield "asdfsfd"
]]
local result = make_google_docstrings(source)
assert.equal(expected, result)
end)
it("works when doing yield + return at once", function() it("works when doing yield + return at once", function()
local source = [[ local source = [[
def items(value):|cursor| def items(value):|cursor|