Skip to content

Fix AttributeError in __repr__/__str__ for partially initialized objects#118

Merged
pavel-kirienko merged 3 commits intomasterfrom
copilot/fix-str-and-repr-errors
Jan 25, 2026
Merged

Fix AttributeError in __repr__/__str__ for partially initialized objects#118
pavel-kirienko merged 3 commits intomasterfrom
copilot/fix-str-and-repr-errors

Conversation

Copy link
Contributor

Copilot AI commented Jan 25, 2026

When objects fail during __init__, debuggers and exception handlers call __repr__() or __str__() on the partially constructed instance. These methods were accessing attributes not yet set, causing cascading AttributeError that obscured the original initialization error.

Changes

Wrapped string formatting in all __repr__ and __str__ methods with try/except to return fallback strings on AttributeError:

  • Serializable types: CompositeType, DelimitedType, Attribute, Constant, PrimitiveType, VoidType, array types
  • Expression types: Any, Boolean, Rational, String, Set

Pattern applied (already used in DSDLDefinition):

def __repr__(self) -> str:
    try:
        return "%s(name=%r, version=%r, ...)" % (self.__class__.__name__, self.full_name, ...)
    except AttributeError:  # pragma: no cover
        return "%s(UNINITIALIZED)" % self.__class__.__name__

Version: 1.24.2 → 1.24.3

Original prompt

This section details on the original issue you should resolve

<issue_title>str and repr can throw</issue_title>
<issue_description>Some objects in pydsdl can throw attribute errors when repr() or str() is called on them This leads to confusing error messaging when tools attempt to log exceptions thrown in initializers. For example, DSDLDefinition throws if the provided arguments are malformed. Seen in the wild is this debug output:

../pydsdl/pydsdl/_namespace.py:204: in read_files
    target_dsdl_definitions = _construct_dsdl_definitions_from_files(
../pydsdl/pydsdl/_namespace.py:345: in _construct_dsdl_definitions_from_files
    output.add(_dsdl_definition.DSDLDefinition.from_first_in(resolved_fp, list(valid_roots)))
../pydsdl/pydsdl/_dsdl_definition.py:116: in from_first_in
    return cls(dsdl_path, cls._infer_path_to_root_from_first_found(dsdl_path, valid_dsdl_roots))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <[AttributeError("'DSDLDefinition' object has no attribute '_name'") raised in repr()] DSDLDefinition object at 0x102d46b10>

    def __init__(self, file_path: Path, root_namespace_path: Path):
        """ """
        # Normalizing the path and reading the definition text

...

Suggested change:

def __str__(self) -> str:
  try:
      return "DSDLDefinition(full_name=%r, version=%r, fixed_port_id=%r, file_path=%s)" % (
          self.full_name,
          self.version,
          self.fixed_port_id,
          self.file_path,
      )
  except AttributeError:
      return "DSDLDefinition(UNINITIALIZED)"
```</issue_description>

<agent_instructions>Don't forget to bump the patch version.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@thirtytwobits</author><body>
DSDLDefinition example was fixed along with change to fix OpenCyphal/pydsdl#109. Keeping this issue open to scrub the code base for similar issues.</body></comment_new>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 25, 2026 09:50
…lized objects

Co-authored-by: pavel-kirienko <3298404+pavel-kirienko@users.noreply.github.com>
Updated badge links in README for documentation.
Copilot AI changed the title [WIP] Fix str and repr methods to handle attribute errors Fix AttributeError in __repr__/__str__ for partially initialized objects Jan 25, 2026
Copilot AI requested a review from pavel-kirienko January 25, 2026 09:54
@pavel-kirienko pavel-kirienko marked this pull request as ready for review January 25, 2026 13:30
@pavel-kirienko pavel-kirienko merged commit 2229ca5 into master Jan 25, 2026
15 checks passed
@pavel-kirienko pavel-kirienko deleted the copilot/fix-str-and-repr-errors branch January 25, 2026 13:30
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

Successfully merging this pull request may close these issues.

str and repr can throw

2 participants