Coder Social home page Coder Social logo

Comments (12)

Stewori avatar Stewori commented on June 20, 2024

I can confirm this is a bug. Note that it is OOP specific. If not in classes, the same scenario appears to work as expected:

def log1(a: Any, b: Any = None) -> None:
    print(a, b)

def log2(a, b: Any = None) -> None:
    print(a, b)

if __name__ == '__main__':
    typechecked('__main__')
    log1(1)
    log2(2)

Thanks for spotting this. However I am currently not able to work on pytypes, so this won't be fixed anytime soon (unless someone steps in and makes a PR).

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

Another thing occurred to me which should be tracked here:
Todo: Check if this also affects @staticmethod, @classmethod and @property.

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

The cause is somewhere in type_util._funcsigtypes.

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

The issue is that type_util._handle_defaults is not self-aware.

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

I think I fixed it on my local machine, but writing tests etc properly takes time. It would be helpful if you could rewrite your code sample above as a test (including @staticmethod etc cases).
However until this is merged, let me point out a workaround:
You can turn off default-inference by
pytypes.infer_default_value_types = False
I checked that this "cures" the issue.

from pytypes.

sjjessop avatar sjjessop commented on June 20, 2024

Is it enough to have tests of using @typechecked to decorate the various kinds of methods, or would you also need tests of calling it on a module (as in the code I originally reported), and/or tests of the underscored functions in type_util?

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

First of all, apologize that the test suite is such a mess. I know it needs refactoring but there are always other priorities.
The test for this should go next to https://github.com/Stewori/pytypes/blob/master/tests/test_typechecker.py#L4454 but as a separate test method. Link this issue like it is frequently done, e.g. in https://github.com/Stewori/pytypes/blob/master/tests/test_typechecker.py#L4762.
Python 3 syntax i.e. the helper class in this case, must go into https://github.com/Stewori/pytypes/blob/master/tests/testhelpers/typechecker_testhelper_py3.py, because test_typechecker still contains Python 2 tests (Python 2 support is kept for Jython).
I think there should only one class be required for this test.

Is it enough to have tests of using @typechecked to decorate the various kinds of methods

Yes. Ideally, simply decorate the helperclass with @typechecked, that will apply to all methods.
Testing of the _ underscore methods is not required (I know it should be but this is not the time to revolutionize the test suite, see #11 ). For now I am fine with just a single symptomatical test that pins this issue, close to your initial example + staticmethod and classmethod cases if they should be affected by this issue (didn't check yet). (With "pin" I mean the test code should fail before the fix and pass after the fix. You can check this using pytypes.infer_default_value_types = False)

If you prefer it, you can also just post the test code here and I'll put it into the right places. OTOH if you file a proper PR you will be credited in the project git history.

from pytypes.

sjjessop avatar sjjessop commented on June 20, 2024

No problem, the only difficulty I had with the test suite was that I didn't leave enough blank lines after my helper class, which caused a completely unrelated test to fail. Stupid comment-based annotations ;-)

@classmethod and @property.setter are affected, @staticmethod isn't (probably no suprises there). Shall I put a staticmethod test in there anyway, for completeness, or do you prefer to have only the tests for this issue?

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

Re testing staticmethod. I am undecided. Do it like you prefer.

from pytypes.

sjjessop avatar sjjessop commented on June 20, 2024

PR build fails on Travis in the way I expected (Python 2.7 and 3.4 pass because the tests are skipped. Python 3.5+ three tests fail). You have the permissions to make the fix in my fork before merging, if you prefer.

from pytypes.

sjjessop avatar sjjessop commented on June 20, 2024

While I'm in here, are you interested in PRs for either of the following minor niggles? I won't be offended if you aren't!

  • Typo in the readme: "It's main features are currently" should have no apostrophe.
  • Quite a bit of trailing whitespace in both the files I edited. I only noticed because I have my text editor set to trim trailing whitespace on save, which created a messy diff.

from pytypes.

Stewori avatar Stewori commented on June 20, 2024

Thanks for the help! Please go ahead.

from pytypes.

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.