Comments (5)
I think the overloads overall need to be different besides just that one change; reading the implementation and docs, it's dependant on chunksize
or iterator
being set that causes it to return a TextFileReader.
from python-type-stubs.
I think the overloads overall need to be different besides just that one change; reading the implementation and docs, it's dependant on
chunksize
oriterator
being set that causes it to return a TextFileReader.
I think, you are completely right. I further think, that a first measure should be, to take care of the ReturnValue, which would help others most. As we only have stubs here, I think changing them will not harm.
from python-type-stubs.
I spent way too much time on this today, and then discovered this issue. I'm not sure how python type checking with overloads can do this right. Maybe @jakebailey or @erictraut have some ideas (see example below):
The design of the API changes the return type to TextFileReader
under any of the following conditions:
chunksize
is specified and integer and notNone
iterator
is specified and set toTrue
So you have to find a way in the overloads to handle all the possible combinations of chunksize
and iterator
, both of which are keyword arguments, and either or both may not be specified. That is reflected in the following table
iterator |
chunksize |
Return Type |
---|---|---|
Not specified | Not specified | DataFrame |
False |
Not specified | DataFrame |
False |
None |
DataFrame |
False |
An integer | TextFileReader |
True |
Not specified | TextFileReader |
True |
None |
TextFileReader |
True |
An integer | TextFileReader |
Not specified | None |
DataFrame |
Not specified | An integer | TextFileReader |
To help me figure this out, I wrote a simple test, where i1
is iterator
and chunksize
is cs
, and a return type of int
corresponds to DataFrame
and a return type of str
corresponds to TextFileReader
:
@overload
def myfun(fake: str, i1: Literal[True], cs: Literal[None]) -> str:
...
@overload
def myfun(fake: str, i1: Literal[True], cs: int) -> str:
...
@overload
def myfun(fake: str, i1: Literal[True]) -> str:
...
@overload
def myfun(fake: str) -> int:
...
@overload
def myfun(fake: str, i1: Literal[False]) -> int:
...
@overload
def myfun(fake: str, i1: Literal[False], cs: Literal[None]) -> int:
...
@overload
def myfun(fake: str, i1: Literal[False], cs: int) -> str:
...
# @overload
# def myfun(fake: str, i1: bool = False, cs: None = None) -> int:
# ...
# @overload
# def myfun(fake: str, i1: bool = ..., cs: int = ...) -> str:
# ...
def myfun(fake: str, i1: bool = False, cs: Optional[int] = None) -> Union[str, int]:
print(f"i1 is {i1} cs is {cs} result is ", end="")
if i1:
if cs is not None:
return "TextFileReader"
else:
return "TextFileReader"
else:
if cs is not None:
return "TextFileReader"
else:
return -1
res1: int = myfun("meh")
res2: int = myfun("meh", i1=False)
res3: int = myfun("meh", i1=False, cs=None)
res4: str = myfun("meh", i1=False, cs=23)
res5: str = myfun("meh", i1=True)
res6: str = myfun("meh", i1=True, cs=None)
res7: str = myfun("meh", i1=True, cs=23)
res8: int = myfun("meh", cs=None)
res9: str = myfun("meh", cs=23)
I could not figure out how to get the last 2 function calls (which correspond to the last 2 lines in the table) to have a proper overload, because in those cases, i1
(equivalently iterator
) is not specified, but cs
is specified. I've commented out some attempts but I'd get complaints about overlapping signatures.
If someone can show me how to get the above to work, then I could make the changes to read_csv()
typing to correspond.
from python-type-stubs.
I think this can be achieved by the following:
@overload
def myfun(fake: str, i1: Literal[True], cs: int | None = ...) -> str:
...
@overload
def myfun(fake: str, i1: Literal[False], cs: int) -> str:
...
@overload
def myfun(fake: str, i1: Literal[False] = ..., cs: None = ...) -> int:
...
@overload
def myfun(fake: str, i1: bool = ..., cs: int = ...) -> str:
...
from python-type-stubs.
Thanks @erictraut . That makes pyright happy, but mypy complains. I'll open up an issue there.
from python-type-stubs.
Related Issues (20)
- Gym stubs are out of date HOT 1
- type of subplots is partially unknown in matplotlib plt.subplots HOT 3
- Module is not callable HOT 3
- [cv2] cv2.add can accept scalar value, not only `Mat`
- pandas read_excel(), DataFrame.iloc[] stubs issues HOT 5
- mypy report error when import cv2
- Incorrect return type on `matplotlib.pyplot.subplot_mosaic`
- [Matplotlib] Uncorrect type-hint in `font_manager.FontProperties` HOT 2
- matplotlib colors.py stub HOT 4
- Why does the dict returned by matplotlib.pyplot.subplot_mosaic have Text as key type? HOT 3
- matplotlib missing markers
- Ignore - Misplaced Ticket
- Add how to install and use section to README HOT 6
- Error in type stubs of matplotlib for 'widgets.Slider' HOT 1
- `cv2` missing function `imwritemulti` HOT 1
- Errors given by pylance on networkx HOT 1
- Run `black` and `isort` automatically or as a hygiene check HOT 1
- Does `utils/build_bundle/build.sh` serve any purpose?
- Deprecated warning: center_of_mass and label have been moved from scipy.ndimage.measurements HOT 13
- Cannot import sklearn.preprocessing.TargetEncoder HOT 1
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 python-type-stubs.