GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add NewType by ilevkivskyi · Pull Request #226 · python/typing

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented May 27, 2016 •
edited by gvanrossum
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Simple minded implementation of NewType (see #189 and python/mypy#1284 (comment)) plus some tests and a short discussion in the PEP.
Also I added Type and ContextManager to the list of types provided by typing.py.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ilevkivskyi added 3 commits May 26, 2016 23:54
@ilevkivskyi
Simple minded implementation of NewType
b612a78
@ilevkivskyi
Add unittests. Add NewType to __all__
2146b23
@ilevkivskyi
Short discusion in PEP plus corrections to lists
473a394
@ilevkivskyi ilevkivskyi mentioned this pull request May 27, 2016
Add NewType() to create simple unique types with zero runtime overhead #189
Closed
@bintoro

bintoro commented May 27, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@ilevkivskyi I know you're intent on maintaining the distinction between types and classes, so I think here "class" is the correct choice, no? EDIT: Actually, never mind. I was too focused on how they simulate instantiation. I think there's just something about the name NewType that's bugging me.

From an introspection POV, an instance of a class with def __call__(self, x): return x would be easier to detect than a plain function while still avoiding the instantiation overhead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented May 27, 2016

Copy link
Copy Markdown
Member

@JukkaL, what do you think of this design?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi

ilevkivskyi commented May 27, 2016

Copy link
Copy Markdown
Member Author

@bintoro

I know you're intent on maintaining the distinction between types and classes, so I think here "class" is the correct choice, no?

I am intentionally a bit "general" here, see my comment from the issue:
"I do not discuss whether NewType('EURtoUSD', Callable[[float], float]) etc. are allowed. If necessary, we could clarify this later."

Concerning your two other points, the idea here is to be really minimal, to allow minimum overhead. In particular your idea with many base classes is almost equivalent to Intersection, that might be implemented later (there is an issue on this).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented May 27, 2016 via email

Copy link
Copy Markdown
Member
Note, see my comment/concern I posted to the corresponding issue. I don't think this is ripe yet...
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed May 27, 2016
View reviewed changes
Comment thread pep-0484.txt Outdated
NewType helper function
-----------------------

There are also situations, where a programmer might want to avoid logical

@gvanrossum gvanrossum May 27, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comma.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ilevkivskyi
response to comments on NewType
3c6371c
gvanrossum
gvanrossum reviewed May 27, 2016
View reviewed changes
Comment thread pep-0484.txt

* Generic, used to create user-defined generic classes

* Type, used to annotate class objects

@gvanrossum gvanrossum May 27, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next one I'll apply right now. They're unrelated to this issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ilevkivskyi ilevkivskyi May 27, 2016

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sure, I just posted a link to this PR to python-dev asking for comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JukkaL

JukkaL commented May 27, 2016

Copy link
Copy Markdown
Contributor

A few things:

  • I'd add explicit language about isinstance not being supported for these types (and why not).
  • What should we call types defined using NewType? Are they "NewType types"?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi
Response to further comments
0605163
@ilevkivskyi

ilevkivskyi commented May 27, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@JukkaL

I'd add explicit language about isinstance not being supported for these types (and why not).

Good point. I added this in new commit

What should we call types defined using NewType? Are they "NewType types"?

Here I am not sure. Maybe ad-hoc types?

EDIT: replaced by the correct quote

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented May 27, 2016 via email

Copy link
Copy Markdown
Member
Let's think some more about the name, it feels important. Even if the function is called NewType.
…
--Guido (mobile)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bintoro

bintoro commented May 28, 2016

Copy link
Copy Markdown
Contributor

Appointed types — because they are only nominally distinct.

ilevkivskyi reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lgautier

lgautier commented May 28, 2016

Copy link
Copy Markdown
  • AdhocType- because (presumably) just created for one situation
  • NamedType - to play on a naming similarity with namedtuple
sietse reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yedpodtrzitko

yedpodtrzitko commented May 29, 2016

Copy link
Copy Markdown

LambdaType

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@SpotlightKid

SpotlightKid commented May 29, 2016

Copy link
Copy Markdown

TypeDef

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sietse

sietse commented Jun 1, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

I like @lgautier's NamedType a lot: to me, it expresses "this is otherwise the same type, but it has a different name (and therefore its own meaning)". Also, like he says, it meshes nicely with namedtuple (and typing.NamedTuple).

How I would read it:

UserID = NamedType('UserID', int)
This is a type of int named UserID


If NamedType does not make it, what about ValueType?

  • ValueType: Scala has the same concept, and calls them Value Classes. It expresses nicely that this type does not say something about the data structure, but about the value it contains.

How I would read it:

UserID = ValueType('UserID', int)
This is an int with a value that is a user ID.

NamedType works better, though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Jun 1, 2016

Copy link
Copy Markdown
Member

I could not follow anything from the Scala ValueType description (no news
there :-).

The explanation "it is the same type with a different name" feels wrong --
if it really was the same name it would be a type alias. In fact it is not
the same type, it has a specific relationship to the original type, which
is exactly that it is a subtype (but not every subtype is a NewType).

I will check with Jukka but unless he has strong objections I am going for
Distinct Type (two words) when we're discussing it in English text, but I
will keep NewType (one function) in code.

On Wed, Jun 1, 2016 at 1:42 AM, Sietse Brouwer [email protected]
wrote:

I like @lgautier https://github.com/lgautier's NamedType a lot: to me,
it expresses "this is otherwise the same type, but it has a different name
(and therefore its own meaning)". Also, like he says, it meshes nicely with
namedtuple (and typing.NamedTuple).

How I would read it:

UserID = NamedType('UserID', int) -> This is a type of int named UserID


If NamedType does not make it, what about ValueType?

  • ValueType: Scala has the same concept, and calls them Value Classes
    http://docs.scala-lang.org/overviews/core/value-classes.html. It
    expresses nicely that this type does not say something about the data
    structure, but about the value it contains.

How I would read it:

UserID = ValueType('UserID', int) -> This is an int with a user ID value
type.

NamedType works better, though.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#226 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ACwrMiamuJVBxyBPxt4bA0yULC0M3MzVks5qHUXcgaJpZM4IoPSY
.

--Guido van Rossum (python.org/~guido)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Jun 2, 2016

Copy link
Copy Markdown
Member

I'm going to abandon the "English description" discussion, and merge this as-is. The new function will be called NewType() and in the discussion we'll use "unique type" (like in this PR).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum
gvanrossum merged commit 807d3a9 into python:master Jun 2, 2016
@gvanrossum gvanrossum mentioned this pull request Jun 6, 2016
Type aliases without implicit conversion ("NewType") python/mypy#1284
Closed
@gvanrossum

gvanrossum commented Jun 6, 2016

Copy link
Copy Markdown
Member

Mypy issue is here: python/mypy#1284

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@ilevkivskyi @bintoro @gvanrossum @JukkaL @lgautier @yedpodtrzitko @SpotlightKid @sietse
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.