Skip to content

Add method to ensure a valid topic name #1238

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
merged 4 commits into from
Oct 7, 2017
Merged

Add method to ensure a valid topic name #1238

merged 4 commits into from
Oct 7, 2017

Conversation

nikeee
Copy link
Contributor

@nikeee nikeee commented Oct 5, 2017

As described in #961, the consumer does not check whether topic names are valid. It does however check if the topic name is a string.
This PR adds a check according to the current kafka source:
https://github.com/apache/kafka/blob/39eb31feaeebfb184d98cc5d94da9148c2319d81/clients/src/main/java/org/apache/kafka/common/internals/Topic.java#L29

That means:

  • A Topic name must not be empty
  • It must not be . or ..
  • It must be shorter than 250 characters
  • It must consist only of a-z, A-Z, '0-9', ., - and _

if not isinstance(topic, six.string_types):
raise TypeError('All topics must be strings')
if len(topic) == 0:
raise TypeError('All topics must be non-empty strings')
Copy link
Owner

Choose a reason for hiding this comment

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

I'd probably make the bottom 4 exceptions ValueError. (The first two make sense w/ TypeError)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@dpkp
Copy link
Owner

dpkp commented Oct 5, 2017

Awesome!

Copy link
Contributor

@jeffwidman jeffwidman left a comment

Choose a reason for hiding this comment

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

Thanks for this. A few comments on the docstrings.

What do you think about adding a test for this new method as well, I would think it'd be very straightforward, especially if you use pytest.mark.parametrize()... ?

""" Ensures that the topic name is valid according to the kafka source.

Raises:
TypeError: if the topic is
Copy link
Contributor

@jeffwidman jeffwidman Oct 5, 2017

Choose a reason for hiding this comment

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

nit: now that you're raising ValueError, this docstring should also be updated. Either clarify what you raise, or just say that you raise on X condidtions, without specifying what gets raise (I'm fine with either given that this is a somewhat buried docstring, other maintainers may have other opinions).

Copy link
Contributor Author

@nikeee nikeee Oct 6, 2017

Choose a reason for hiding this comment

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

I tried to adapt to the style of the stuff of the project that I removed (see this).

So I'd remove the description of why a ValueError/TypeError is thrown. Is this okay?

Copy link
Owner

Choose a reason for hiding this comment

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

I usually only add a docstring to a private method if it isn't obvious from the code what or why it is doing. The audience for private methods is primarily developers with access to the source code. But the public methods end up getting pulled out into HTML docs on kafka-python.readthedocs.org , so those are much more important.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dpkp right now the SubscriptionState docs are not visible on http://kafka-python.readthedocs.io/en/master/apidoc/modules.html, do you want to change that?

I'm fine with dropping the docstring from the private method, it reduces the risk of the docstring going out of sync with the code.

- a non-str or
- an empty string or
- the topic name is '.' or '..' or
- the topic name does not consist of ASCII-characters/'-'/'_'/'.'
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably entirely remove this part of the docstring since the raising for invalid values now happens in _ensure_valid_topic_name()

Copy link
Contributor Author

@nikeee nikeee Oct 6, 2017

Choose a reason for hiding this comment

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

The internal method raises something. Shouldn't this be documented in the public method that uses this method (since it uses it only for that)? The checks are only encapsulated in a separate method to make it easier to reuse the checks later on.

I would not remove these raise-Docstrings but update them accordingly (matching the style mentioned above).

Copy link
Owner

Choose a reason for hiding this comment

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

I would keep them in the docstring here, but update re ValueError

@nikeee
Copy link
Contributor Author

nikeee commented Oct 7, 2017

@dpkp @jeffwidman Can you take another look on this?

@dpkp dpkp merged commit 30ba2c1 into dpkp:master Oct 7, 2017
@dpkp
Copy link
Owner

dpkp commented Oct 7, 2017

thanks!

@nikeee nikeee deleted the issue-961 branch October 7, 2017 22:53
88manpreet pushed a commit to Yelp/kafka-python that referenced this pull request Jul 16, 2018
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.

3 participants