python - 如何键入仅使用构造函数可能返回类型的子集的函数?
问题描述
我有一个使用 GeoPandas 的函数GeoDataFrame
,它是 Pandas 的子类DataFrame
。GeoPandas 的read_file()
构造函数可以创建 aGeoDataFrame
或DataFrame
实例,具体取决于使用的选项,但我的函数需要 aGeoDataFrame
才能工作。我试图这样输入提示:
import geopandas as gpd
from geopandas.geodataframe import GeoDataFrame
def my_geographic_function(input_gdf: GeoDataFrame) -> Set[Tuple[int, int]]:
# do stuff
return the_result
input_gdf = gpd.read_file("my_input_file.geojson")
result = my_geographic_function(input_gdf)
但是,我的类型检查器(VS Code 中的 Pylance)报告说这是不正确的:
Argument of type "DataFrame | GeoDataFrame" cannot be assigned to parameter "input_gdf" of type "GeoDataFrame" in function "my_geographic_function"
提示只有类型的子集read_file()
可以返回是有效输入的正确方法是my_geographic_function()
什么?
解决方案
推荐阅读
- mongodb - Mongo db连接未建立
- wordpress - 在给定的经度和纬度半径范围内有哪些帖子?
- continuous-integration - 会议升级过程中面临的问题
- r - 用不同 DF 中的匹配列替换列中的值
- mysql - Powershell SSH 上的 MYSQLDUMP (POSH)
- c# - 对 ASP.NET Core ApiController 的删除请求不起作用
- java - ProcessBuilder 在 Linux 上运行 jar
- javascript - 将现有的 json 数据显示为类似表格的矩阵
- php - WooCommerce - 从价格范围查询中隐藏产品类别
- php - 如何在 Symfony 4 控制台命令中获取应用程序根路径