Skip to content

Version 3 to version 4 migration hint when accessing removed methods and attributes #2124

@VeckoTheGecko

Description

@VeckoTheGecko

Just parking this code here until we have a migration guide that we can link to.

class MyClass:
    def some_method(): ...

    def __getattr__(self, name):
        ...  # default behaviour of __getattr__, if already defined

        # if the code hasn't already returned here, then the names weren't expected and we should raise an AttributeError

        _attribute_error_with_v3_to_v4_hint(self, name, deprecated_names=["some_removed_name"])


# in a utils file
def _attribute_error_with_v3_to_v4_hint(obj, name, *, deprecated_names):
    hint = ""
    if name in deprecated_names:
        hint += (
            f" HINT: '{name}' is removed in v4 of Parcels, see our migration guide "
            "for more info LINK_TO_MIGRATION_GUIDE."
        )

    msg = f"'{type(obj).__name__}' object has no attribute '{name}'.{hint}"
    raise AttributeError(msg)


# tests file
import pytest


def test_v3_to_v4_migration_hint_removed_method_attr():
    obj = MyClass()
    with pytest.raises(
        AttributeError,
        match="'MyClass' object has no attribute 'some_removed_name'. "
        "HINT: 'some_removed_name' is removed in v4 of Parcels, see our migration guide "
        "for more info LINK_TO_MIGRATION_GUIDE.",
    ):
        obj.some_removed_name

    with pytest.raises(
        AttributeError,
        match="'MyClass' object has no attribute 'some_name_that_never_existed'.",
    ):
        obj.some_name_that_never_existed

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhackathon2025Good issues for the hackathon at the Parcels anniversary event in 2025.v4

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Backlog

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions