Replies: 3 comments
-
|
A few thoughts. First, on guided annotations. For non-main files, that don't have an intended execution behavior (or those that do, once imported), a series of beartype hooks / flags within the file could provided granular direction. For example, inferring the entire file, inferring all usages of certain variables everywhere, inferring certain functions, doing those operations only to function definitions, and/or doing it to variable definitions within functions, omitting annotating when clearly defined by an assignment from a known type, or passed into a well-typed function, etc. And whence done, removes those annotations. No cli required. As for the quality of the generated types themselves, I suspect most would prefer semantic aliases of longer things, for sanity's sake. Ex, when encountering a type hint or a variable name with a gnarly hint for the first time, prompting for the user for a shorthand would clear these up. Like with git rerere, these responses can be cached for all the subsequent imports, which should sanely propagate type hints throughout the dependency chain (especially if the inference started with a main executable root, which then recursively imports all its dependencies, automatically, as normal). (Or searches the import for an already defined shorthand for that said type) Ex: Lastly, in git we trust. If performing operations on the host pc is still too tenuous, then all of this modification could be done in-memory, the imported files stored in memory, and then only dump/print() the patch file with the diff, leaving it to the user to apply said patch file, and handle the likes/dislikes appropriately. |
Beta Was this translation helpful? Give feedback.
-
|
...heh. I've created a nightmare. In so many words, this is what you are saying, @JWCS. Help! Somebody help! Save me from the feature creep horrors I have unwittingly unleashed on us all! I was trying to have fun and now I've created a shambolic megalith of empty hopes and shattered dreams resembling I agree with what you are saying, @JWCS. Somebody should do all of those things. Let us please acknowledge the bitter reality, however: that somebody will probably not be @leycec. I haven't even finished deep It's been years of blood-curdling @beartype development at this point. @beartype is still nowhere close to being where we all expected @beartype to be. Dynamic code generation is rough stuff, indeed. I guess that's why only @beartype does that stuff, huh? Emoji face sighs. 😮💨 Thankfully, I've been thinking. It's weird but stay with me. @leycec thinks for once. And what I'm thinking is... everything @thiswillbeyourgithub, @JWCS, and probably everybody else wants here is probably best achieved in some new third-party package that lives outside @beartype. I'm thinking of a Plum-style arrangement, where: |
Beta Was this translation helpful? Give feedback.
-
|
Hi everyone, sorry for the time I took to answer. TL;DRWe all three (@JWCS and @leycec) seem to have come to the same conclusion: modifying the source code brings a lot of questions whereas simply writing a txt file containing a record of all the types hints for each function (and organised such that each function is described only once in the dump of course) would be amazingly useful while remaining seemingly doable. I have too much time:
Didn't know that, thanks!
I disagree. So far a type hint in python means "the developper says that thing should be of this type". But the idea I'm suggesting allows to reverse this: the actual run encountered this type. This is new because AFAWK nobody has yet done something that infers type as the code runs then writes it to the file. So you don't need the code to be bugfree, as you can use this to debug your code. Frequently, I have to read someone else's code and it's bad, has bad variable names, bad comments, and especially: it lacks type hints. This makes it even harder to figure out what exactly is going on in some parts of the code. What I'm suggesting could solve this: just run the code and then you have descriptive typehints that make it easier to figure out what's going on.
Apart from using git, this can be solved by adding a comment after the hint, like Bearhint. Actually instead of adding hints if the idea is just to make the code more readable by adding descriptive hints then they can just be comments instead of actual type hints.
I was thinking
First of all you seem nice :). But yeah a solution for this is to just add comments instead of hardcording the hints. Actually I usable product would be simply a package
Yeah and just add an output file to store it and it would already be amazing.
We came to the same conclusion, hurray! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This bright idea 💡 😮 comes courtesy @thiswillbeyourgithub at this intriguingly devilish comment:
That's... a superb question, actually! Yeah. Sounds totally feasible and (more importantly) useful. As you suggest, writing type hints is an unctuous chore that oscillates between:
Social media mostly agrees with the latter viewpoint – especially the
/r/Pythonsubreddit. Gods, do those guys really loathe type hints. They're not wrong. But they're not right, either. I tell myself everybody only hates type hints because they're not using @beartype. 🥹Back on Track: It's Time to Suite Up, Boys
By which I mean... this idea sounds compelling, dangerous, and exotic. I'm in! Immediate concerns that flit about my capacious brainpan, which is admittedly just empty air at this point:
beartype.claw.annotate_this_package()wouldn't be enough. Inevitably, everybody would want its older siblingsbeartype.claw.annotate_package(),beartype.claw.annotate_packages(), andbeartype.claw.annotate_all()to say "Hey, bro!" and join in the scrum. @leycec is now holding his head while the wife shakes her fist at GitHub.beartype.claw.beartype_*()hooks. I foresee grossimportlibconflicts as competing import hooks fight over scarceimportlocks. Import hooks aren't really composable in Python. They should be. Everyone wants and expects them to be. But... they just aren't. Python import hooks are a non-deterministic rugby match in an unlicensed hippo pit between former best friends over the fate of your package. There can be only one winner. But there can be many, many losers. Basically everybody ends up in the mouth of a hippo.importlibmachinery and type-checking wrapper functions already generated by @beartype? You... kinda just don't..pyfiles in your on-disk package by somehow globally injecting inferred type hints into those files? What if your package already has type hints? Should @beartype just silently overwrite them (i.e., "@beartype knows best, bro. Trust the Bear or everything crashes.") or avoid overwriting anything entirely (i.e., "@beartype knows nothing, bro. Trust the Bear and everything crashes.")? More importantly, can @beartype even physically modify any of the.pyfiles in your on-disk package? Usually, the answer is: "Nope. @beartype ain't got user permissions to do nuffin'." Unless your package is installed to some user-localsite-packages/directory in editable mode withpip install -e ., in which case... sure. Maybe. Maybe @beartype could actually do that. But probably not. Static analysis of Python code is wickedly hard. Personally, I wouldn't trust @beartype to safely inject type hints into arbitrary submodules through file rewriting. You shouldn't either.Good Solutions vs. Bad Ideas: Round 1. Fight!!
The solution to 1. and 2. above is clear: define a new
BeartypeConf(claw_is_infer_hints=True)configuration option. When enabled, @beartype will dynamically generate additional runtime logic in type-checking wrapper function to infer type hints for passed and returned objects that lack existing type hints. In other words, we piggy-back type hint inference onto the existingbeartype.clawimport hook infrastructure by just adding a new configuration option. So far, so good.The solution to 3., 4., and 5. is probably interrelated. Depending on how we define "patching," this is either really easy or NP-ultimate-hard. Really easy sounds preferable, right? The Really Easy Way is for @beartype to just print out or log all inferred type hints. Then users can manually "patch" those inferred type hints into their codebase at their leisure. The NP-ultimate-hard Way is for @beartype to actually try automating the patching by rewriting user codebases. For the aforementioned reasons (e.g., "undo"-ability, finalization, file permissions, parsing), that would end up being kinda awful even if we could figure out how to do it.
print()Statements: Pretty Sure @beartype Can Do ThatIn light of the above, would @beartype just printing out or logging all inferred type hints satisfy your use case? Would world-shattering scientists such as yourself be equally satisfied or would everybody riot and burn down the @beartype issue tracker? Does everybody trust @beartype to try patching their packages with inferred type hints, despite the high likelihood that doing so would destroy their packages forever?
In short, I really hope my userbase is tracking changes with
git. 😆Beta Was this translation helpful? Give feedback.
All reactions