wrye-bash / wrye-bash

A swiss army knife for modding Bethesda games.
https://wrye-bash.github.io
GNU General Public License v3.0
455 stars 79 forks source link

General code improvements/refactoring #634

Open lojack5 opened 1 year ago

lojack5 commented 1 year ago

This is meant to be a tracker for general code improvements that aren't big enough issues to warrant their own issue, and not small enough to just make the changes directly. Most of these initial issues are thoughts I had while writing type-stubs and noticing places for improvements.

Tick off items if they've been addressed. If we decided to do nothing about it, additionally mark it as such by striking it through.

Utumno commented 1 year ago

There is a big discussion on Flags in #480 + a few merges like 87c7a4cc66bddfc4d18511023e4dc41633c86043 - conclusion was that indeed IntFlag can't serve as a drop in replacement

lojack5 commented 1 year ago

Yeah, the biggest stopper that I ran into while investigating is that IntFlags are ints, which are an immutable type. Pretty much prevents us from every providing a flags.isHidden = True interface. The best we could get is compound assignment operators, flags |= flags.isHidden. And once py 3.11 comes we're loosing access to the flag enums on instances, so that would have to change to flags |= FlagClass.isHidden, and testing for truthyness would have to change to if FlagClass.isHidden in flags:. Overall I like the mutableness of our flags. My Idea though is we might be able to implement some metaclass magicry to make definitions easier, like:

class _MarkerFlags(Flags):
    visible: bool
    can_travel_to: bool
    show_all_hidden: bool

That'd take some work to get right though, messing with metaclasses has all sorts of pitfalls, and doing it in a way so typecheckers see the flag names as bools while also being able to define them the way you want is even weirder.

Utumno commented 1 year ago

Re: flags yes - see also remarks in 87c7a4cc66bddfc4d18511023e4dc41633c86043. Bottom line those are better left alone for now.

I will be adding some todos that I run across in 543 (that is anywhere) in this issue that you might be interested into - it is especially around converters on which I had to plunge as I am trying to finalize 543 - there is some pretty complex code in there: