Skip to content

Method registered members carry an empty str payload, so Method.GET == GET is False #870

Description

@JarryShaw

pcapkit.const.http.method.Method is a StrEnum, but every registered member carries an empty str payload, so comparing one to its own value is False:

Method.GET   value='GET'  str(Method.GET)=''  len=0   Method.GET == 'GET': False

Cause is pcapkit/const/http/method.py:48, inside the class's own __new__:

obj = str.__new__(cls)          # no argument, so the str content is ''

str.__new__(cls) with no second argument builds an empty string, so the member's str content never carries the value even though _value_ does. Every one of the 40 registered members is affected. Reproduced on main at c411d072a and on the current head of #869 alike — it predates both.

Consequence: any caller treating a Method as the string it is (Method.GET == 'GET', '%s' % Method.GET, an f-string, a dict keyed by the verb, str.startswith) silently sees an empty string. The .name and .value attributes are correct, which is why this has gone unnoticed.

Note the interaction with #869: that PR gives unregistered members a real payload (str(Method('frob')) == 'frob'), which leaves registered and unregistered members inconsistent until __new__ is fixed here. Fixing it changes what 40 public members compare equal to, so it is a behaviour change in its own right and belongs in its own review rather than being folded into #869 — see #869 (comment).

Check whether pcapkit/const/ftp/command.py's Command.__new__ and the other hand-rolled __new__s under pcapkit/const/ have the same shape before fixing, and pin the result with a test asserting str(member) == member.value for every member of every str-valued registry.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssues reporting a defect (set by the bug report template; a default, not an assessment)constRegenerated IANA or vendor constant tables; members keep their numeric valuesfixPull requests that fix a defect (fix: subject prefix)

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions