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

Documentation of exception types #750

Open
IllDepence opened this issue Sep 5, 2023 · 1 comment
Open

Documentation of exception types #750

IllDepence opened this issue Sep 5, 2023 · 1 comment

Comments

@IllDepence
Copy link

I’m looking for a documentation of exception types.

The web documentation only briefly mentions “instance of YAMLError or of its subclass” without further details.

In the source I could so far only find yaml/_yaml.pyx lines 14–23, which appears to be a list of the possible exception classes, but without an explanation of what they mean.

Does such a documentation exist (specifically for/within the Python package or somewhere else)?

Thanks. :‌‌)

@mikey-
Copy link

mikey- commented Dec 18, 2024

While looking for this info recently; I found that I was able to inspect the subclasses fairly easily with IPython, which looks like so:

In [1]: import yaml

In [2]: { YamlErrorSubclass: YamlErrorSubclass.__subclasses__() for YamlErrorSubclass in yaml.error.YAMLError.__subclasses__()}
Out[2]:
{yaml.error.MarkedYAMLError: [yaml.scanner.ScannerError,
  yaml.parser.ParserError,
  yaml.composer.ComposerError,
  yaml.constructor.ConstructorError],
 yaml.reader.ReaderError: [],
 yaml.resolver.ResolverError: [],
 yaml.emitter.EmitterError: [],
 yaml.serializer.SerializerError: [],
 yaml.representer.RepresenterError: []}

Think of it like this:

graph TD
    yaml.scanner.ScannerError --> yaml.error.MarkedYAMLError --> yaml.error.YAMLError --> Exception
    yaml.parser.ParserError --> yaml.error.MarkedYAMLError
    yaml.composer.ComposerError --> yaml.error.MarkedYAMLError
    yaml.constructor.ConstructorError-->yaml.error.MarkedYAMLError  
    yaml.reader.ReaderError --> yaml.error.YAMLError;
    yaml.resolver.ResolverError --> yaml.error.YAMLError;
    yaml.emitter.EmitterError --> yaml.error.YAMLError;
    yaml.serializer.SerializerError --> yaml.error.YAMLError;
    yaml.representer.RepresenterError --> yaml.error.YAMLError;
Loading

I also took a look at the class definitions for each exception and I think I can break it down some more:

All exception types in pyyaml are type of error.YAMLError, which is simply just a type of Exception. It seems to me that the exception types in pyyaml are created to raise module specific exceptions (Look in the modules to get information about what causes specific exceptions to be raised). So, generally - if an exception is raised: the type of exception will tell you which module raised exception. On top of what I've covered: there are two exceptions worth talking about in pyyaml. They are: error.MarkedYAMLError and reader.ReaderError- - both of which inherit from error.YAMLError, but also add their own properties so that specific contextual information can be relayed via the error message. Both error.MarkedYAMLError and reader.ReaderError define their own __init__ and __str__ methods:

  • __init__ method: defines and initialises contextual information regarding errors.
  • __str__ method: controls the presentation of contextual information regarding errors

As you have probably already noticed, some exceptions are subclasses of error.MarkedYAMLError, so they'll have that sweet sweet contextual information as well.

I hope this information is useful and accurate. Apologies if I haven't understood something correctly :o

<3 mikey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants