Skip to content
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

New type based on int() created with typing.NewType is not consistent #77351

Closed
avanov mannequin opened this issue Mar 28, 2018 · 3 comments
Closed

New type based on int() created with typing.NewType is not consistent #77351

avanov mannequin opened this issue Mar 28, 2018 · 3 comments
Labels
stdlib Python modules in the Lib dir

Comments

@avanov
Copy link
Mannequin

avanov mannequin commented Mar 28, 2018

BPO 33170
Nosy @avanov

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-03-28.15:21:36.743>
created_at = <Date 2018-03-28.14:51:28.263>
labels = ['library']
title = 'New type based on int() created with typing.NewType is not consistent'
updated_at = <Date 2018-03-28.15:21:36.742>
user = 'https://github.com/avanov'

bugs.python.org fields:

activity = <Date 2018-03-28.15:21:36.742>
actor = 'avanov'
assignee = 'none'
closed = True
closed_date = <Date 2018-03-28.15:21:36.743>
closer = 'avanov'
components = ['Library (Lib)']
creation = <Date 2018-03-28.14:51:28.263>
creator = 'avanov'
dependencies = []
files = []
hgrepos = []
issue_num = 33170
keywords = []
message_count = 3.0
messages = ['314598', '314604', '314607']
nosy_count = 1.0
nosy_names = ['avanov']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue33170'
versions = ['Python 3.6']

@avanov
Copy link
Mannequin Author

avanov mannequin commented Mar 28, 2018

From my understanding of the docs section on new types, https://docs.python.org/3/library/typing.html#newtype the new type based on
int() should just pass the value into the base constructor. However,

PercentDiscount = NewType('PercentDiscount', int)

>>> PercentDiscount(50) == int(50)
True

>>> int('50') == int(50)
True

>>> PercentDiscount('50') == PercentDiscount(50)
False

@avanov avanov mannequin added the stdlib Python modules in the Lib dir label Mar 28, 2018
@avanov
Copy link
Mannequin Author

avanov mannequin commented Mar 28, 2018

Logically, I would expect it to behave similarly to

>>> class PercentDiscount(int): pass

>>> PercentDiscount('50') == PercentDiscount(50)
True

@avanov
Copy link
Mannequin Author

avanov mannequin commented Mar 28, 2018

Ok, after further reading, I see that NewType creates an identity stub.

@avanov avanov mannequin closed this as completed Mar 28, 2018
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

0 participants