fix: replace unsafe 'is' comparisons with '==' for strings #201
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Descrição
Este PR corrige o issue #192, substituindo todas as comparações inseguras de strings que utilizam o operador
ispelo operador==nos contratos de validação.O problema: O operador
isverifica identidade de objeto (se dois objetos são o mesmo na memória), enquanto==verifica igualdade de valores. Para comparações de strings, especialmente constantes, devemos usar==pois o comportamento deiscom strings pode ser imprevisível devido ao string interning do Python.Mudanças Propostas
message is CONSTANTpormessage == CONSTANTem 9 arquivos de contratos de validaçãobool_validation_contract.py,brazilian_document_validation_contract.py,collections_validation_contract.py,commons_validation_contract.py,contract.py,credit_card_validation_contract.py,email_validation_contract.py,strings_validation_contract.py,url_validation_contract.pyChecklist de Revisão
Comentários Adicionais
Esta é uma correção importante de segurança e boas práticas. Embora o código atual provavelmente funcione devido ao string interning do Python para literais, usar
ispara comparar strings é considerado uma má prática e pode levar a bugs sutis e difíceis de debugar em cenários específicos.A correção é backward-compatible e não altera o comportamento funcional da biblioteca - apenas torna o código mais robusto e seguro.
Issue Relacionada
Fixes #192