Bug: CaseInsensitiveDict (event.headers) returns True for both == and != against the same mapping

Offen Anfängerfreundlich
#8,948 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
backend

Rechercherichtung

Beginne damit, CaseInsensitiveDict in den Dienstprogrammen für Datenklassen zu finden, und lies die __eq__-Implementierung zusammen mit dem hier beschriebenen Verhalten beim Vergleich von Dictionaries. Reproduziere die Abweichung mit dem bereitgestellten Snippet und prüfe oder ergänze anschließend gezielte Tests für == und !=, einschließlich des Falls APIGatewayProxyEvent.headers. Fertig ist die Aufgabe, wenn Ungleichheit konsistent das Gegenteil des Ergebnisses von Gleichheit liefert und die gezielten Tests bestehen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug triage
Expected Behaviour

!= is the opposite of ==. If event.headers == {"Content-Type": "application/json"} is True, then event.headers != {"Content-Type": "application/json"} is False.

Current Behaviour

Both comparisons return True. CaseInsensitiveDict overrides __eq__ (lowercasing the other mapping's keys) but not __ne__. dict.__ne__ does not call __eq__; it compares the raw lowercased keys with the other mapping's original keys, so any mapping with non-lowercase keys compares as different.

Code snippet
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from aws_lambda_powertools.utilities.data_classes.common import CaseInsensitiveDict

headers = CaseInsensitiveDict({"Content-Type": "application/json"})
print(headers == {"Content-Type": "application/json"})  # True
print(headers != {"Content-Type": "application/json"})  # True  <- expected False

event = APIGatewayProxyEvent({"headers": {"Content-Type": "application/json"}})
print(event.headers != {"Content-Type": "application/json"})  # True  <- expected False
Possible Solution

Define __ne__ in terms of __eq__:

def __ne__(self, other):
    result = self.__eq__(other)
    return result if result is NotImplemented else not result

I have this ready on a branch with tests (parametrized ==/!= cases for mixed-case, lowercase and CaseInsensitiveDict operands, non-equal mappings and a non-mapping, plus an event.headers case through APIGatewayProxyEvent): https://github.com/khaledix3/powertools-lambda-python/tree/fix/case-insensitive-dict-ne

Two of the new tests fail on develop and all pass with the fix; the rest of tests/unit, tests/functional/event_handler and tests/functional/validator give the same results before and after. Happy to open the PR once this is triaged.

I used an AI assistant (Claude) to find and fix this, and ran the snippet and tests myself.

Steps to Reproduce
  1. Run the snippet above on develop (0bbc252) or the latest release.
  2. Observe that == and != both return True for the same mapping.
Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

latest (3.41.0, also develop 0bbc252)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

Vorherrschende Sprache
Python
Sterne
3.3k
Forks
511
Ø Merge
2 T. 23 Std.
Gemergte PRs (30 T.)
50

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus aws-powertools/powertools-lambda-python

Alle Issues in aws-powertools/powertools-lambda-python

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.