Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to import tomllib for Python 3.11 or greater #1004

Closed
wants to merge 2 commits into from
Closed

Try to import tomllib for Python 3.11 or greater #1004

wants to merge 2 commits into from

Conversation

xofbd
Copy link

@xofbd xofbd commented Mar 27, 2024

Describe your changes

As of Python 3.11, the standard library includes a library for reading TOML files. This allows for new enough versions of Python to fall back on the stdlib version if toml is not installed.

Issue number

Closes #X

Checklist before requesting a review


📚 Documentation preview 📚: https://jupysql--1004.org.readthedocs.build/en/1004/

As of Python 3.11, the standard library includes a library for reading
TOML files. This allows for new enough versions of Python to fall back
on the stdlib version if toml is not installed.
@xofbd xofbd requested a review from edublancas as a code owner March 27, 2024 13:54
@edublancas edublancas added the feature Adds a new feature label Mar 27, 2024
@edublancas
Copy link

please check the CI, you're missing an entry in the changelog


from jinja2 import Template


try:
import toml
except ModuleNotFoundError:
toml = None
if sys.version_info >= (3, 11):
import tomllib as toml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does tomlib have the same api as the toml package?

also, I think more changes are needed because the function that loads toml, still checks that the package is installed, and has a try-catch statement that checks for an error:


@requires(["toml"])
def load_toml(file_path):
    """
    Returns toml file content in a dictionary format
    and raises error if it fails to load the toml file
    """
    try:
        with open(file_path, "r") as file:
            content = file.read()
            return toml.loads(content)
    except toml.TomlDecodeError as e:
        raise parse_toml_error(e, file_path)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the API, for the loads function, then both toml and tomllib have the same API. However, the exception is different, TomlDecodeError vs TOMLDecodeError.

Thanks for pointing this out, let me take another crack at this.

@xofbd xofbd closed this Apr 23, 2024
@xofbd xofbd deleted the tomllib branch April 23, 2024 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adds a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants