GithubHelp home page GithubHelp logo

Type annotations or not? about patma HOT 7 OPEN

gvanrossum avatar gvanrossum commented on June 29, 2024
Type annotations or not?

from patma.

Comments (7)

gvanrossum avatar gvanrossum commented on June 29, 2024

The argument for keeping the syntax of patterns the same as that for expressions, but giving it a (quite!) different semantics, seems weak, since the semantics really are so different already -- e.g. 1|2 in a pattern doesn't mean the same as 3, it matches either 1 or 2.

Presumably this means that we shouldn't allow any other operators (e.g. 1+2 should be disallowed), except unary - (for negative numbers only). Oh, and if we support complex numbers we should allow + to add a real and an imaginary part, e.g. 1+0j.

from patma.

gvanrossum avatar gvanrossum commented on June 29, 2024

More arguments against type annotations:

  • Annotations are for static checkers, but matching is runtime behavior.
  • What should case [x: list[int]] check? isinstance(x, list) and all(isinstance(a, int) for a in x)?
  • What about case [x: list[T]]?

from patma.

ilevkivskyi avatar ilevkivskyi commented on June 29, 2024

FWIW, impossibility to use list[int] is the main reason why I don't like the annotation syntax.

from patma.

viridia avatar viridia commented on June 29, 2024

So what was the final judgment on this? I know it was marked as rejected, but it's not clear from the conversation why.

from patma.

gvanrossum avatar gvanrossum commented on June 29, 2024

Mostly because of TOOWTDI, and because we don't know what we would do with things like list[int] -- Python currently disallows isinstance(x, list[int]), and it would be yet another source of inconsistency.

from patma.

viridia avatar viridia commented on June 29, 2024

OK after reading this a couple more time I think I misunderstood what was being proposed. You were proposing to use a syntax similar to type annotations to replace or substitute the pattern expression syntax proposed in other threads.

However, that is not what I thought you were proposing - I thought the proposal was to add type annotations in addition to the pattern expression. Something like this:

  case [a := list: list[int], b := str]:

or possibly

  case [a: list[int] := list, b := str]:

What this is saying is that the pattern expression matches a list, and should be assigned to the variable a; and, oh by the way you static checkers, the type of that list just happens to be list[int]. Like all other type annotations, it has no effect on runtime and is only meaningful to static type checkers.

The problem with this, of course, is that it doesn't work for patterns not enclosed in brackets:

  case a := list: list[int]:

...because now you have an ambiguous grammar on colon.

from patma.

brandtbucher avatar brandtbucher commented on June 29, 2024

Yeah, I think the "obvious" way to type extracted variables is to explicitly annotate them before the match block. Colons/parentheses already have multiple meanings in patterns, and giving them any more just looks like unreadable screen-grit-soup to me. 🙂

from patma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.