Giter VIP home page Giter VIP logo

Comments (10)

ambv avatar ambv commented on July 22, 2024

The Hack approach would basically be:

with typing.off:
  ...

The pylint approach has much more granularity but requires unique IDs to all checks.

from typing.

gvanrossum avatar gvanrossum commented on July 22, 2024

I like the simple with-statement. I think it is possible to write something that is both a decorator and a context manager. IIRC the unittest.mock package does this, so perhaps we could use the same name. When used as typing.off the cute name is fine; but after "from typing import off" it's too short IMO. (It turns off what?)

from typing.

JukkaL avatar JukkaL commented on July 22, 2024

The with statement is somewhat slow in CPython (the same applies to things like List[int] and Undefined(...)) and introduces an extra block nesting level, which makes code more difficult to read.

We could also have a comment that disables/enables all errors:

# typing: off
1 + 'x'  # don't report an error
# typing: on
1 + 'x'  # report an error

The comment should probably only extend until the end of the block that introduced it:

if foo:
    # typing: off
    1 + 'x'   # don't report an error
1 + 'x'  # report an error

from typing.

gvanrossum avatar gvanrossum commented on July 22, 2024

Hm. I'm not so keen on introducing more magic comments. See #35

from typing.

gvanrossum avatar gvanrossum commented on July 22, 2024

So we're introducing magic comments, but we might want to have something for stub modules to declare that a module exports absolutely anything: https://mail.python.org/pipermail/python-ideas/2015-January/031381.html

from typing.

JukkaL avatar JukkaL commented on July 22, 2024

Mypy has a related concept for classes as an enhancement proposal. It would allow any attribute that's not explicitly defined to be accessed with type Any, but explicitly defined attributes could still have non-Any types. Obviously this can be implemented by defining dummy __getattr__ and __setattr__, but having special construct this may be less ugly and more explicit. For example:

from typing import Dynamic

class A(Dynamic):
    x = 0

a = A()
a.x = ''  # Error, x is an integer
x.y = ''  # Okay, y is not explicitly defined so it defaults to Any

Here are more syntax ideas:

@dynamicclass
class A: ...

@openclass
class A: ...

class A:
    * = Undefined(Any)  # Based on Guido's idea

I like the idea of having something like this, both for modules and classes. It would make it easy to define partial, work-in-progress stubs with static types only for the most commonly used features, and everything else would default to Any.

from typing.

gvanrossum avatar gvanrossum commented on July 22, 2024

There's probably a mypy task left here, and there's a TODO for the class decorator in my typing.py, but the decision is made so I'm closing this:

  • To disable for the rest of the file:
# type: ignore
  • To disable per class or method, use @no_type_check

from typing.

JukkaL avatar JukkaL commented on July 22, 2024

The PEP draft is still somewhat unclear on this. Here's how I'd like # type: ignore to work.

Case 1: ignore until end of block

if c:
    # type: ignore
    x.foo = 1  # don't complain about this
x.bar = 2  # potentially complain about this

Case 2: ignore until end of file (special case of 1)

x.foo = 1  # this is type checked

# type: ignore

x.bar = 2 # this is ignored by the type checker
...

Case 3: ignore single line (comment on a non-empty line)

x.foo = 2  # type: ignore      # no complaints

x.bar = 3  # this is type checked

Case 4: ignore import statement

If we ignore an import statement, don't try to type check the imported module and don't complain if we can't find it. Give type Any to any imported names:

from foo import x  # type: ignore    # should this be # type: Any?
import foo2  # type: ignore

x.bar()   # okay, x has type Any
foo2.bar()  # okay, foo2 has type Any

from typing.

refi64 avatar refi64 commented on July 22, 2024

So, is there still no way to ignore a small chunk of text? This would definitely work:

x = 1
if True:
    # type: ignore
    # this always runs and never gets type checked
    x = 'abc'

but it feels ugly to me. I would like some kind of fake context manager:

with ignore_type:
    ...

but that would probably have speed issues.

from typing.

gvanrossum avatar gvanrossum commented on July 22, 2024

But how often do you really need that? Have you run into examples where you need this or are you just musing on potential use cases? The context manager feels too heavy to me (though if we want it, we could probably make the no_type_check decorator also act as a context manager, similar to some unittest decorators). Why couldn't you use x = 'abc' # type: Any?

from typing.

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.