Giter Club home page Giter Club logo

Comments (7)

last-partizan avatar last-partizan commented on August 17, 2024 2

Now the hard part!

I'll try to add types for fields, my previous idea was working with pyright but i'll need to do some changes for mypy :)

What's preferred way of adding types, inline or in separate .pyi files?

from django-phonenumber-field.

francoisfreitag avatar francoisfreitag commented on August 17, 2024 2

I vote for inline, they provide information when reading the source that way, without having to jump back and forth between the .pyi or relying on IDE support.

from django-phonenumber-field.

francoisfreitag avatar francoisfreitag commented on August 17, 2024

We should add mypy to the CI to verify types, before to officially support type hints.
If you’re up for it, that would be greatly appreciated!

from django-phonenumber-field.

last-partizan avatar last-partizan commented on August 17, 2024

Sure, but first, some questions.

  1. what exactly do you want to check in CI? You want to type-check all source code, or you want to type check some test code to ensure that added types are working? I used second approach when adding types to pytest-decopatch: https://github.com/smarie/python-decopatch/pull/27/files
  2. Do you want mypy specifically, or pyright is fine too? I'm more used to pyright, and it has nice --outputjson option, so we can test against snapshot as in my linked PR.

Currently, pyright has better type-checking capabilities, here's example run on this package.

> src/django-phonenumber-field  562-mypy-ci > mypy phonenumber_field

phonenumber_field/widgets.py:21: error: Incompatible types in assignment (expression has type "None", variable has type Module)  [assignment]
Found 1 error in 1 file (checked 8 source files)

> src/django-phonenumber-field  562-mypy-ci > pyright phonenumber_field
/home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:30:38 - error: No parameter named "region" (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:30:26 - error: Object of type "Widget" is not callable (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:32:26 - error: Object of type "Widget" is not callable (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:35:21 - error: Cannot assign member "input_type" for type "type[Widget]"
    Member "input_type" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:35:21 - error: Cannot assign member "input_type" for type "Widget"
    Member "input_type" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:40:52 - error: Cannot access member "as_national" for type "PhoneNumber"
    Member "as_national" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/formfields.py:40:52 - error: "as_national" is not a known member of "None" (reportOptionalMemberAccess)
/home/serg/src/django-phonenumber-field/phonenumber_field/phonenumber.py
  /home/serg/src/django-phonenumber-field/phonenumber_field/phonenumber.py:110:51 - error: Cannot access member "is_valid" for type "str"
    Member "is_valid" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/phonenumber.py:110:27 - error: Cannot access member "format_as" for type "str"
    Member "format_as" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/phonenumber.py:110:73 - error: Cannot access member "raw_input" for type "str"
    Member "raw_input" is unknown (reportGeneralTypeIssues)
/home/serg/src/django-phonenumber-field/phonenumber_field/serializerfields.py
  /home/serg/src/django-phonenumber-field/phonenumber_field/serializerfields.py:28:46 - error: Cannot access member "is_valid" for type "str"
    Member "is_valid" is unknown (reportGeneralTypeIssues)
/home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:19:12 - warning: Import "babel" could not be resolved from source (reportMissingModuleSource)
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:43:28 - error: Argument of type "tuple[str, str]" cannot be assigned to parameter "__object" of type "tuple[Literal[''], Literal['---------']]" in function "append"
    "str" cannot be assigned to type "Literal['']"
    "str" cannot be assigned to type "Literal['---------']" (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:121:16 - error: Cannot assign member "_region" for type "PhoneNumber"
    Member "_region" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:121:16 - error: Cannot assign member "_region" for type "Literal['']"
    Member "_region" is unknown (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:155:62 - error: Argument of type "int | None" cannot be assigned to parameter "country_code" of type "int" in function "region_codes_for_country_code"
    Type "int | None" cannot be assigned to type "int"
      Type "None" cannot be assigned to type "int" (reportGeneralTypeIssues)
  /home/serg/src/django-phonenumber-field/phonenumber_field/widgets.py:174:62 - error: Argument of type "int | None" cannot be assigned to parameter "country_code" of type "int" in function "region_codes_for_country_code"
    Type "int | None" cannot be assigned to type "int"
      Type "None" cannot be assigned to type "int" (reportGeneralTypeIssues)
16 errors, 1 warning, 0 informations

But, if we're planning to only verify that after adding types - we're getting what we expecting - both are good choices.

from django-phonenumber-field.

last-partizan avatar last-partizan commented on August 17, 2024

#563

Here's basic mypy integration, and it already passes the tests with only two minor changes.

from django-phonenumber-field.

francoisfreitag avatar francoisfreitag commented on August 17, 2024

That’s great, thanks! ⭐

  1. Ideally, I would rather type check all the source code, so that type hints help directly when reading the code. That might be a second step, I’m guessing it requires more work.
  2. Most errors reported by pyright look like false positives, not issues of interest. I would rather stick with mypy, since that’s what Django might be using in the future according to django/deps#65, and the target of django-stubs.

from django-phonenumber-field.

HasanAshab avatar HasanAshab commented on August 17, 2024

Any update? PR not merged yet.

from django-phonenumber-field.

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.