Skip to content

Conversation

@lev-blit
Copy link
Contributor

I have made things!

as written in the linked issue - slug_field isn't really None, it's only None as the argument to the __init__, but is asserted to not be None in the first line of the method

Related issues

closes #207

Copy link
Contributor

@intgr intgr left a comment

Choose a reason for hiding this comment

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

Maybe the parameter should be changed in DRF upstream instead?


class SlugRelatedField(RelatedField[_MT, str, str]):
slug_field: str | None
slug_field: str
Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the attribute is good 👍

def __init__(
self,
slug_field: str,
slug_field: str | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

But the parameter isn't really | None if there's an assert prohibiting it.

I'd suggest opening a PR to upstream https://github.com/encode/django-rest-framework instead to remove the default value.

Copy link
Contributor

Choose a reason for hiding this comment

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

@intgr intgr self-assigned this Sep 23, 2025
@lev-blit
Copy link
Contributor Author

I'm not against making the upstream patch, but currently this is the correct typing

would you rather wait for upstream to accept my change before accepting a PR to change the typing?

@intgr
Copy link
Contributor

intgr commented Sep 23, 2025

currently this is the correct typing

I think type hints should help write correct code, not so much strictly follow what is written in the function signature.

If you cannot pass None or omit this parameter, then type-hinting it as x | None = None is unhelpful and I would argue incorrect.

would you rather wait for upstream to accept my change

The attribute change can be merged immediately. The parameter should be left as-is regardless of whether it's changed upstream.

A comment in the .pyi file would be helpful though, explaining why it's different from function signature.

@lev-blit
Copy link
Contributor Author

I agree with your sentiment regarding type hints :)

I'll fix this PR to include only the attribute change to str instead of str | None :)

Copy link
Contributor

@intgr intgr left a comment

Choose a reason for hiding this comment

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

Thanks!

@intgr intgr changed the title fix slug_field typing according to match the implementation Fix SlugRelatedField.slug_field type: cannot be None Sep 23, 2025
@intgr intgr merged commit 89df910 into typeddjango:master Sep 23, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Should SlugRelatedField.slug_field be optional?

2 participants