Git Product home page Git Product logo

Comments (5)

pheuer avatar pheuer commented on June 16, 2024

I forget the error this raises, but:

def fcn(x:None| int = None):
    if x is None:
         x = 0

    y = x + 1

This raises a mypy error because it thinks x can still be None on the last line, and it drives me crazy since this is a pretty common design pattern!

from plasmapy.

namurphy avatar namurphy commented on June 16, 2024

Huh, the mypy error I get from that is no-untyped-def since there's no return type annotation. mypy is usually good at type narrowing, assuming it can infer what's in the if statement. I'm curious if the error you ran into is happening because there's some dynamical typing happening. 🤔 @pheuer — could you ping me the next time you run into an error like this?

I'm wondering if it may be associated with untyped decorators (like @validate_quantities). Otherwise it's sometimes possible to define a TypeGuard.

Thank you for bringing this up!

from plasmapy.

pheuer avatar pheuer commented on June 16, 2024

Ah, maybe it is related to decorators? I'll keep an eye out for next time. Here's another pytest specific one

def test_fcn(ex : Exception | None):
    with pytest.raises(ex):
            ...

For some reason mypy is looking for an overloaded function (raising call-overload) for pytest.raises that has Exception as its class, except isn't that just the default (and only?) signature for that function?

from plasmapy.

namurphy avatar namurphy commented on June 16, 2024

Interesting! I believe the pytest API changed recently so that pytest.raises no longer accepts None, but we get the call-overload error even after removing the | None. (The source file of pytest.raises contains a few signatures of pytest.raises that are decorated with @overload.) In any case, this is a good motivation to ignore call-overload in all test files since the advantages of having the more in-depth type hints don't outweigh the extra complicatedness of having to do all that. Thanks!

from plasmapy.

jwreep avatar jwreep commented on June 16, 2024

When trying to add particlewise to particle_collections.is_category, the static type check for mypy kept raising issues with this code:

if (
not self.allow_custom_particles
and isinstance(particle, ParticleList)
and any(particle.is_category("custom", particlewise=True)) # type: ignore[arg-type]
):
raise InvalidParticleError(
f"{self.callable_.__name__} does not accept CustomParticle "
f"or CustomParticle-like inputs."
)

The "issue" is that the individual Particle class has a method without particlewise as a keyword, so it would only return a bool, which is not iterable. The third condition can only be called, however, given that it passes the check for isinstance(particle, ParticleList), so this cannot be an issue (unless you can somehow pass that second check otherwise?).

This was frustrating trying to debug. The only way I could think was to use nested conditionals, which then ruff complains about. The solution I used was just to tell mypy to ignore it # type: ignore[arg-type].

I don't know if there's any more clever way.

from plasmapy.

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.