Skip to content

Make InertiaRequest inherit from HttpRequest (#78) #84

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

Merged

Conversation

kennyputman
Copy link
Contributor

Follow up on #78. Makes InertiaRequest inherit from Django's HttpRequest in order to improve compatibility with other libraries.

@BrandonShar BrandonShar requested a review from Copilot July 22, 2025 01:49
Copilot

This comment was marked as outdated.

@BrandonShar
Copy link
Collaborator

Thanks @kennyputman ! Per the copilot review comment, now that we're extending HttpRequest I wonder if we need to put any thought into how InertiaRequest is essentially a decorator.

@kennyputman
Copy link
Contributor Author

Thanks for checking that out @BrandonShar. I updated the code to use inheritance with super(). I think typical inheritance would be preferable over the decorator style one from the first pull request. It should be more compatible with the HttpRequest other libraries may expect.

Do you have any thoughts on this?

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modifies the InertiaRequest class to inherit directly from Django's HttpRequest instead of using composition with a __getattr__ delegation pattern. This change improves compatibility with other Django libraries that expect request objects to be instances of HttpRequest.

  • Changes InertiaRequest from composition to inheritance pattern
  • Updates property methods to use proper inheritance approach
  • Replaces attribute delegation with direct dictionary copying

def __getattr__(self, name):
return getattr(self.request, name)
super().__init__()
self.__dict__.update(request.__dict__)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice, this is a clean way of handling this; I thought it might be more complex.

Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

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

@kennyputman this looks great, thanks for fixing it up! Just a couple minor changes and I think we're good to merge!

inertia/http.py Outdated

@property
def headers(self):
return self.request.headers
return super().headers
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like we can remove this method entirely now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely, thanks for the catch!

return self.request.inertia.all() if hasattr(self.request, "inertia") else {}
inertia_attr = self.__dict__.get("inertia")
return (
inertia_attr.all() if inertia_attr and hasattr(inertia_attr, "all") else {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like the refactor equivalent should be something like

return self.inertia.all() if hasattr(self, "inertia") else {}

does that look right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I originally tried something like that, but it ends up in a RecursionError as the property repeatedly calls itself.

The current code is a bit of a workaround as it seems the more idiomatic way is to rename the attribute to self._inertia, but that would require updating share.py. It could be done with something similar to this.

def share(request, **kwargs):
    if not hasattr(request, "_inertia"):
        request._inertia = InertiaShare()

    request._inertia.set(**kwargs)

Then it could be called using

return self._inertia.all() if hasattr(self, "_inertia") else {}

Do you want me to make these updates or stick to the current code?

Copy link
Collaborator

Choose a reason for hiding this comment

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

ahhh yeah I get that. Ok, I think this makes sense and maybe we'll come back to it later. Thanks for double checking!

@kennyputman kennyputman force-pushed the fix-inertiarequest-inherits-httprequest branch from 714d934 to e372e11 Compare July 26, 2025 14:09
Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

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

Looks good!

@BrandonShar BrandonShar merged commit e242994 into inertiajs:main Jul 27, 2025
3 checks passed
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.

2 participants