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
Invalid is
check in credit card validation
#1317
Comments
Probably works because of some if card_number.brand in (PaymentCardBrand.visa, PaymentCardBrand.mastercard) |
I guess it depends if equality is acceptable compared to identity? |
I don't think magic works with is, and I'm kinda glad of that:
|
Good to confirm. |
Assuming this is resolved. |
Sadly not resolved, code is still if card_number.brand is (PaymentCardBrand.visa or PaymentCardBrand.mastercard): which won't work if |
Yes, I was just confirming the logic is broken. @mike-hart you might want to submit a PR referencing this issue with the fix. |
The use of I'll replace it with an A bit more background on enums:
class PaymentCardBrand(str, Enum):
amex = 'American Express'
mastercard = 'Mastercard'
visa = 'Visa'
other = 'other'
b = PaymentCardBrand.visa
b is PaymentCardBrand.visa
#> True
b == PaymentCardBrand.visa
#> True
b = 'Visa'
b is PaymentCardBrand.visa
#> False
b == PaymentCardBrand.visa
#> True (This would not work with the current PaymentCardBrand which doesn't inherit from str) I don't know if |
Ah, either way, I added this - #1320 - but since you propose a different approach I guess it's less valuable now. Thanks for responding/reopening. |
https://github.com/samuelcolvin/pydantic/blob/master/pydantic/types.py#L669
Assuming brand was reduced to the integers, and we had 3 brands, then this is the equivalent of:
3 is (1 or 2)
, which you'd hope were false,1 is (1 or 2)
which you'd hope were true, and2 is (1 or 2)
which you'd also hope were true. However,or
short-circuits, so these reduce to3 is 1
,1 is 1
, and2 is 1
. This means the preferred outcome for the first two cases works, but it would fail for mastercard.Perhaps this should be
I think this was added relatively recently - 0c57937#diff-470d0730db2e82eaaf27ad3655a667b3R582 - so hopefully it hasn't affected too many people.
The text was updated successfully, but these errors were encountered: