psf / psf/requests

LookupDict does not implement full contract of the dict class

Open
#6,238 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.5k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

LookupDict absolutely includes all of the dict methods. In particular, it allows the use of __setitem__ and other things from the dict class. It specifically does only one thing, which is override __getitem__ to allow returning None.

So it's not ok to replace the dict with object in this case, even though the requests codebase doesn't use that functionality.

Thanks for reporting this issue though, and I hope you keep reading through the codebase!

Originally posted by @Lukasa in https://github.com/psf/requests/issues/3848#issuecomment-277197592

This is unfortunately a false statement. Take a look on the following checks:

In [1]: from requests.status_codes import codes

In [2]: codes
Out[2]: <lookup 'status_codes'>

In [3]: codes.keys()
Out[3]: dict_keys([])

In [4]: list(codes.keys())
Out[4]: []

If LookupDict pretends to quack like a duck it should do so. Or it should not derive from the dict if it works even worse:

In [8]: from requests.status_codes import codes

In [9]: codes["my_code"] = 700

In [10]: list(codes.keys())
Out[10]: ['my_code']

In [11]: codes["my_code"]

In [12]: 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the requests.status_codes.codes and LookupDict examples from the issue, then trace the LookupDict implementation and its dict behavior. Clarify the intended contract before changing anything; done means the chosen contract is represented consistently by lookup, assignment, and key-enumeration behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.