Comments (7)
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.
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.
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.
Sure, but first, some questions.
- 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
- Do you want
mypy
specifically, orpyright
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.
Here's basic mypy integration, and it already passes the tests with only two minor changes.
from django-phonenumber-field.
That’s great, thanks! ⭐
- 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.
- Most errors reported by
pyright
look like false positives, not issues of interest. I would rather stick withmypy
, 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.
Any update? PR not merged yet.
from django-phonenumber-field.
Related Issues (20)
- Remove check for .mo files
- Form field does not auto-detect region from international number HOT 2
- Italian translations incomplete HOT 5
- Usage with django ninja HOT 5
- Multiple countrycodes in `PhoneNumberField` HOT 3
- Make the model/formfield region kwarg the default selection in PhoneNumberPrefixWidget HOT 1
- Validation error with wrong locale HOT 1
- bug in python 3.10 HOT 1
- Extension information is lost during serialization HOT 8
- [Question] Best way to generae US phonenumber for testing HOT 1
- validate_international_phonenumber does not validate correctly at some cases HOT 1
- Pydantic Serialization HOT 1
- Use Django's "normal" validation, not hardcoded in __init__() HOT 9
- Turkmenistan phone numbers has been updated HOT 1
- The valid phonenumber is not accepted by this package HOT 1
- 0493 prefix invalid numbet Australia HOT 1
- Handling of special (non-geo) phone numbers
- python-phonenumbers should be a requirement HOT 1
- django-phonenumber-field[phonenumberslite] requires phonenumbers on migration HOT 1
- Support numbers as phone numbers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-phonenumber-field.