Repository navigation
Configuration Refactor - #496
Conversation
1757d90 to
3d76ecf
Compare
|
|
|
This looks like a great start. Obviously, the failing tests will need to be cleaned up, but I think this has a lot of potential. As things progress, more stuff can be moved into the validators. For example, the code which splits the |
That's an interesting idea, I think it makes a lot of sense to do this. We probably need to rename validators tho', perhaps they could just be called ConfigOptions as they do validation and post processing too.
I could do with a better way of handling this too. At the moment if the user provided this it would be overwritten, given it is a reference to the config file the user is providing they can't ever give me anything useful here :) It would be easy to handle something like this in the current code by making a validator that errors if it does exist and then handle the required work in a
I needed a two pass, so the class MarkdownExtension(BaseValidator):
def check(self, value):
if value is not None:
raise ValidationError("..")
def post_process(self, config, key_name):
config['markdown_extensions_extra'] = {}However, I wonder if the concepts should be separated, perhaps into two dictionaries one that holds the config, one that holds internal state and then a session object could bind them together and be passed around. |
3688e7a to
2cda316
Compare
This change breaks up the config parsing into a set of validators. This makes it somewhat easier to follow and add extra checks. As a result we now also catch a few config errors we wouldn't have before. The code probably still needs some tidying up, but it works well.
|
I think I'm happy enough with this to land it now. @waylan or anyone else want to review before I do? I expect this will need follow up work, but it should all be much more minor. |
There was a problem hiding this comment.
You have this backwards. The private method should be the one called internally and the public method should be the one overridden by subclasses. Switch validate and _validate.
There was a problem hiding this comment.
The reason for this, is that I didn't want to have to do the following on each subclass.
def validate(self, value):
value = super(CLASS, self)._validate(value)
if value is None:
returnSo, validate only calls the private subclassed method if the value isn't None.
There was a problem hiding this comment.
I do feel like I am missing an obvious way to do this in a better way, but I can't quite think of it.
There was a problem hiding this comment.
What I think he meant was that the subclasses should override a method called validate. Flip the names of what is exposed and what is not. Subclasses should not override private methods. You shouldn't have to use super as you are still using things the same.
There was a problem hiding this comment.
Oh, of course. I get it. Sorry, it's been a long day. That makes so much more sense, not sure why I didn't do this before.
There was a problem hiding this comment.
The original recommendation was based on the original code. It wasn't understood that there was an aversion to call _validate. I personally find overriding _validate more evil than calling it, but personal preference and all that ;). I think it just depends on how people approach the language, my background in coding (C/C++) steers me towards the latter. But I get some people in python view _ as don't touch, unless your using a named tuple, where they were forced to expose the class methods that way :).
There was a problem hiding this comment.
If you are curious, I scope it like this and it's the pattern used in most projects I'm involved with.
- Underscore class methods within the class and subclasses only
- Underscore functions (defined outside a class) used within the file only
- Underscore files (like
_file.py) within the project only.
I rarely used double underscore anywhere, other than __init__ an so on.
There was a problem hiding this comment.
Yeah, I don't care much for do_* names either. But in this case, validate defines how the validator works, but do_validation runs the validation, which seems reasonable. You could use run_validation or run_validators. Just run works too.
There was a problem hiding this comment.
Thanks for explaining. I realize object oriented approaches can be completely different in other languages. As there is no strict rule on how to use underscore methods, I guess it really depends on the community of authors you work with. It's nice to understand different approaches. I will definitely keep this approach in mind when looking at other people's Python projects moving forward.
There was a problem hiding this comment.
@waylan Yup. makes sense. I might just go with one of those :)
@facelessuser You are welcome, there are few hard rules here so my way is just one approach. pylint will complain if you access private properties on an object tho, that's one of the reasons for my bias.
0010b09 to
5d21d84
Compare
There was a problem hiding this comment.
Typo: run_validation not run_validatuon -- needs to be fixed in some the subclasses too.
I guess I expected this method to be called validate and the method currently called validate to be run_validation, but whatever. I guess run_validation and post_validation is more consistent.
a116b84 to
89071bf
Compare
|
@waylan @facelessuser Thanks both for your input. I'm going to merge this and continue iterating on it in master over time. I think it's a big enough improvement and a big enough PR that I don't want to continue in a branch much longer and make bigger. However, I'd still be very interested in more feedback - so if you have anything, either comment here or open a new issue. Thanks again! |
This name should explain what it does a bit more.
This change breaks up the config parsing into a set of validators. This
makes it somewhat easier to follow and add extra checks. As a result we
now also catch a few config errors we wouldn't have before.
The code probably still needs some tidying up, but it works well.