Skip to content

Commit

Permalink
Fix when receiving a None object inside of a list for requirements (#490
Browse files Browse the repository at this point in the history
)

* Fix when receivinngn a None object inside of a list for requirements

* Add test for remove_all_inner_nones
  • Loading branch information
marcelotrevisani authored Sep 20, 2023
1 parent 7636a80 commit c31edb8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions grayskull/strategy/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import re
from collections.abc import Mapping
from pathlib import Path
from tempfile import mkdtemp
from typing import Dict, Iterable, List, Optional
Expand Down Expand Up @@ -480,12 +481,25 @@ def get_metadata(recipe, config) -> dict:
}


def remove_all_inner_nones(metadata: Dict) -> Dict:
"""Remove all inner None values from a dictionary."""
if not isinstance(metadata, Mapping):
return metadata
for k, v in metadata.items():
if not isinstance(v, list):
continue
metadata[k] = [i for i in v if i is not None]
return metadata


def update_recipe(recipe: Recipe, config: Configuration, all_sections: List[str]):
"""Update one specific section."""
from souschef.section import Section

metadata = get_metadata(recipe, config)

for section in all_sections:
metadata[section] = remove_all_inner_nones(metadata.get(section, {}))
if metadata.get(section):
if section == "package":
package_metadata = dict(metadata[section])
Expand Down
30 changes: 30 additions & 0 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
get_url_filename,
merge_pypi_sdist_metadata,
normalize_requirements_list,
remove_all_inner_nones,
remove_selectors_pkgs_if_needed,
sort_reqs,
update_recipe,
)
from grayskull.utils import PyVer, format_dependencies, generate_recipe

Expand Down Expand Up @@ -1326,3 +1328,31 @@ def test_sort_reqs():

assert sort_reqs(original_deps) in [sorted_deps_orig, sorted_deps_alpha]
assert sort_reqs(original_deps_38) in [sorted_deps_orig_38, sorted_deps_alpha_38]


@patch("grayskull.strategy.pypi.get_metadata")
def test_metadata_pypi_none_value(mock_get_data):
mock_get_data.return_value = {
"package": {"name": "pypylon", "version": "1.2.3"},
"build": {"test": [None]},
}
recipe = Recipe(name="pypylon")
update_recipe(
recipe,
Configuration(name="pypylon", repo_github="https://github.com/basler/pypylon"),
("package", "build"),
)
assert recipe["build"]["test"] == []


@pytest.mark.parametrize(
"param, result",
[
({"test": [None, None, None]}, {"test": []}),
({"test": [None, "foo", None]}, {"test": ["foo"]}),
({"test": [None, "foo", None, "bar", None]}, {"test": ["foo", "bar"]}),
({"test": [None, "foo", None, "bar", None, None]}, {"test": ["foo", "bar"]}),
],
)
def test_remove_all_inner_none(param, result):
assert remove_all_inner_nones(param) == result

0 comments on commit c31edb8

Please sign in to comment.