4 ms·
I've been away from Django for a few years, and hope to come back one day. One thing I'm curious about, coming from well-typed JS codebases using Flow, is the
by rattray 8y ago
I've been away from Django for a few years, and hope to come back one day.
One thing I'm curious about, coming from well-typed JS codebases using Flow, is the quality of typings in major projects like Django, Pandas, etc. Are their APIs well-typed? Can I get quality types for my models?
- akx 8y agoThe PyCharm IDE has extra support for Django to enable some additional type inference, and it works like an absolute (pun intended) charm. Beyond that, the Django core isn't type-annotated yet likely because of Python version support, but I'm sure you can get typeshed pyis or something.
- yhoiseth 8y agoThere's a discussion on the mailing list about this, last updated a few months ago: https://groups.google.com/forum/#!topic/django-developers/trTEbURFhEY/discussion https://groups.google.com/forum/#!topic/django-developers/tr...
- goatlover 8y agoWith Pandas, typically the important data types are the series data type for columns in a DataFrame or what not. Which Python will infer when you read in the data, or manually construct. These are data types provided by the Numpy library. And you can manually convert columns as needed with Pandas functions. If you mean the object itself, then Python is strongly typed and will throw an error if you try to run the wrong method or access the wrong property on an object (although it will let you assign new ones, and it will let you add new columns to DataFrames of any data type, because that's very useful). If you mean in the method or function calls, then often a parameter will allow different types because you might want to pass in the column name, a list of columns, a function or something else that makes sense for that particular parameter depending on what the method/function does. Pandas has a lot of overloaded parameters because it's a big library that covers a lot of use cases for tabular data.
- rattray 8y agoSorry, I meant static types, like mypy
- goatlover 8y agoRight, my point was that static typing doesn't seem like a good fit for a library like Pandas, which needs to be flexible enough to handle a wide range of use cases for tabular data where you're cleaning, reshaping, etc. At least not without a total rewrite. With Flow in JS, how would a JSON library for similar purposes work where you don't know what kind of data it will be and you will be doing a lot of transformations on it?
- pushtheenvelope 8y agoI think designing for static types changes the design of a library’s api. So, in this case, Pandas would likely have to evolve (handwave) in some fashion, if it were to support Mypy types. One way Mypy could help do this is by implementing a feature like Type Providers https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/type-providers/ https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/typ...
- rattray 8y agoIf you _really_ don't know what type of data you have, you type it as `mixed`. But if you _do_ know the shape of your data, you write your own types, and tell the library about those types (eg; with generics). Think like `my_df: DataFrame<MyDataShape> = pandas.DataFrame(my_data)` where you manually define the shape of `MyDataShape`. Scala is a language that is often used for data processing, and is statically typed. Depending on the situation, codegen can also be useful in these situations, eg; https://github.com/typeorm/typeorm https://github.com/typeorm/typeorm EDIT: in any case, this certainly answers my question :-) sounds like they're not there yet, and perhaps not even moving in that direction yet.