your-tools / tbump

Bump software releases
BSD 3-Clause "New" or "Revised" License
158 stars 22 forks source link

Unable to parse the pyproject.toml file. #156

Closed vipcxj closed 1 year ago

vipcxj commented 1 year ago

here is the exception stack:

  File "d:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\__main__.py", line 3, in <module>
    main()
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\cli.py", line 245, in main
    run(args)
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\cli.py", line 129, in run
    config_file = get_config_file(
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\config.py", line 222, in get_config_file
    res.get_config()
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\config.py", line 69, in get_config
    res = from_parsed_config(parsed)
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\config.py", line 258, in from_parsed_config
    validate_basic_schema(parsed)
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\tbump\config.py", line 180, in validate_basic_schema
    tbump_schema.validate(config)
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\schema.py", line 405, in validate
    nvalue = Schema(svalue, error=e, ignore_extra_keys=i).validate(value, **kwargs)
  File "D:\develop\mamba\envs\ipywebcam-dev\lib\site-packages\schema.py", line 366, in validate
    return type(data)(o.validate(d, **kwargs) for d in data)
TypeError: Array.__init__() missing 1 required positional argument: 'trivia'

I typed the command: tbump current-version This is my pyproject.toml:

[tool.tbump]
field = [
    { name = "channel", default = "" },
    { name = "release", default = "" },
]
file = [
    { src = "pyproject.toml", version_template = "version = \"{major}.{minor}.{patch}{channel}{release}\"" },
    { src = "ipywebcam/_version.py" },
]

I have debug the command, And found the problem. return type(data)(o.validate(d, **kwargs) for d in data) This line cause the exception. It's in schema.py line 366. I found data here is not a native python list, but tomlkit.items.Array.

class Array(Item, _CustomList):
    """
    An array literal
    """

    def __init__(self, value: list, trivia: Trivia, multiline: bool = False) -> None:
        super().__init__(trivia)
        ...

You can see to construct a Array instance, you must provide a trivia as argument.

But why data is not a list? I found when you call ConfigFile.get_parsed, it return a value with toml special type instance, which caused the issue.

So I think to fix this issue, the config returned by ConfigFile.get_parsed should be clean to a pure python instance with list and dict.

Here is a quick fix:

class PyprojectConfig(ConfigFile):
    """Represent a config inside a pyproject.toml file,
    under the [tool.tbump] key
    """

    def __init__(self, path: Path, doc: TOMLDocument):
        super().__init__(path, doc)

    def get_parsed(self) -> dict:
        try:
            # tool_section = self.doc["tool"]["tbump"].value
            # use unwrap here. then we will get a plain python object
            tool_section = self.doc.unwrap()["tool"]["tbump"]
        except KeyError as e:
            raise InvalidConfig(parse_error=e)

        return tool_section.value  # type: ignore
hugetim commented 1 year ago

I'm seeing the same issue.

vipcxj commented 1 year ago

@dmerejkowsky When will the new version be released? Even though pr was merged, it was still the old version on pip for months

dmerejkowsky commented 1 year ago

tbump 6.10.0 has been released and contains this fix. Enjoy :)